Esempio n. 1
0
        internal static void UpdateMenuItem(AToolbar toolbar,
                                            ToolbarItem item,
                                            int?menuItemIndex,
                                            Context context,
                                            Color?tintColor,
                                            PropertyChangedEventHandler toolbarItemChanged,
                                            List <IMenuItem> menuItemsCreated,
                                            List <ToolbarItem> toolbarItemsCreated,
                                            Action <Context, IMenuItem, ToolbarItem> updateMenuItemIcon = null)
        {
            IMenu menu = toolbar.Menu;

            item.PropertyChanged -= toolbarItemChanged;
            item.PropertyChanged += toolbarItemChanged;

            IMenuItem menuitem;

            Java.Lang.ICharSequence newTitle = null;

            if (!String.IsNullOrWhiteSpace(item.Text))
            {
                if (tintColor != null && tintColor != Color.Default)
                {
                    var             color       = item.IsEnabled ? tintColor.Value.ToAndroid() : tintColor.Value.MultiplyAlpha(0.302).ToAndroid();
                    SpannableString titleTinted = new SpannableString(item.Text);
                    titleTinted.SetSpan(new ForegroundColorSpan(color), 0, titleTinted.Length(), 0);
                    newTitle = titleTinted;
                }
                else
                {
                    newTitle = new Java.Lang.String(item.Text);
                }
            }
            else
            {
                newTitle = new Java.Lang.String();
            }

            if (menuItemIndex == null)
            {
                menuitem = menu.Add(0, Platform.GenerateViewId(), 0, newTitle);
                menuItemsCreated?.Add(menuitem);
                toolbarItemsCreated?.Add(item);
            }
            else
            {
                if (menuItemsCreated == null || menuItemsCreated.Count < menuItemIndex.Value)
                {
                    return;
                }

                menuitem = menuItemsCreated[menuItemIndex.Value];

                if (!menuitem.IsAlive())
                {
                    return;
                }

                menuitem.SetTitle(newTitle);
            }

            menuitem.SetEnabled(item.IsEnabled);
            menuitem.SetTitleOrContentDescription(item);

            if (updateMenuItemIcon != null)
            {
                updateMenuItemIcon(context, menuitem, item);
            }
            else
            {
                UpdateMenuItemIcon(context, menuitem, item, tintColor);
            }

            if (item.Order != ToolbarItemOrder.Secondary)
            {
                menuitem.SetShowAsAction(ShowAsAction.Always);
            }

            menuitem.SetOnMenuItemClickListener(new GenericMenuClickListener(((IMenuItemController)item).Activate));

            if (!System.Maui.Maui.IsOreoOrNewer && (tintColor != null && tintColor != Color.Default))
            {
                var view = toolbar.FindViewById(menuitem.ItemId);
                if (view is ATextView textView)
                {
                    if (item.IsEnabled)
                    {
                        textView.SetTextColor(tintColor.Value.ToAndroid());
                    }
                    else
                    {
                        textView.SetTextColor(tintColor.Value.MultiplyAlpha(0.302).ToAndroid());
                    }
                }
            }
        }
        void OnCreate(
            Bundle savedInstanceState,
            ActivationFlags flags)
        {
            Profile.FrameBegin();
            _activityCreated = true;
            if (!AllowFragmentRestore)
            {
                // Remove the automatically persisted fragment structure; we don't need them
                // because we're rebuilding everything from scratch. This saves a bit of memory
                // and prevents loading errors from child fragment managers
                savedInstanceState?.Remove("android:support:fragments");
            }

            Profile.FramePartition("Xamarin.Android.OnCreate");
            base.OnCreate(savedInstanceState);

            Profile.FramePartition("SetSupportActionBar");
            AToolbar bar = null;

#if __ANDROID_29__
            if (ToolbarResource == 0)
            {
                ToolbarResource = Resource.Layout.Toolbar;
            }

            if (TabLayoutResource == 0)
            {
                TabLayoutResource = Resource.Layout.Tabbar;
            }
#endif

            if (ToolbarResource != 0)
            {
                try
                {
                    bar = LayoutInflater.Inflate(ToolbarResource, null).JavaCast <AToolbar>();
                }
#if __ANDROID_29__
                catch (global::Android.Views.InflateException ie)
                {
                    if ((ie.Cause is Java.Lang.ClassNotFoundException || ie.Cause.Cause is Java.Lang.ClassNotFoundException) &&
                        ie.Message.Contains("Error inflating class android.support.v7.widget.Toolbar") &&
                        this.TargetSdkVersion() >= 29)
                    {
                        Internals.Log.Warning(nameof(FormsAppCompatActivity),
                                              "Toolbar layout needs to be updated from android.support.v7.widget.Toolbar to androidx.appcompat.widget.Toolbar. " +
                                              "Tabbar layout need to be updated from android.support.design.widget.TabLayout to com.google.android.material.tabs.TabLayout. " +
                                              "Or if you haven't made any changes to the default Toolbar and Tabbar layouts they can just be deleted.");

                        ToolbarResource   = Resource.Layout.FallbackToolbarDoNotUse;
                        TabLayoutResource = Resource.Layout.FallbackTabbarDoNotUse;

                        bar = LayoutInflater.Inflate(ToolbarResource, null).JavaCast <AToolbar>();
                    }
                    else
                    {
                        throw;
                    }
#else
                catch
                {
                    throw;
#endif
                }

                if (bar == null)
#if __ANDROID_29__
                { throw new InvalidOperationException("ToolbarResource must be set to a Android.Support.V7.Widget.Toolbar"); }
#else
                { throw new InvalidOperationException("ToolbarResource must be set to a androidx.appcompat.widget.Toolbar"); }
#endif
            }
            else
            {
                bar = new AToolbar(this);
            }

            SetSupportActionBar(bar);

            Profile.FramePartition("SetContentView");
            _layout = new ARelativeLayout(BaseContext);
            SetContentView(_layout);

            Profile.FramePartition("OnStateChanged");
            Xamarin.Forms.Application.ClearCurrent();

            _previousState = _currentState;
            _currentState  = AndroidApplicationLifecycleState.OnCreate;

            OnStateChanged();

            Profile.FramePartition("Forms.IsLollipopOrNewer");
            if (Forms.IsLollipopOrNewer)
            {
                // Allow for the status bar color to be changed
                if ((flags & ActivationFlags.DisableSetStatusBarColor) == 0)
                {
                    Profile.FramePartition("Set DrawsSysBarBkgrnds");
                    Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
                }
            }
            if (Forms.IsLollipopOrNewer)
            {
                // Listen for the device going into power save mode so we can handle animations being disabled
                Profile.FramePartition("Allocate PowerSaveModeReceiver");
                _powerSaveModeBroadcastReceiver = new PowerSaveModeBroadcastReceiver();
            }

            ContextExtensions.SetDesignerContext(_layout);
            Profile.FrameEnd();
        }
Esempio n. 3
0
        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            AToolbar bar = _toolbar;

            // make sure bar stays on top of everything
            bar.BringToFront();

            base.OnLayout(changed, l, t, r, b);

            int barHeight = ActionBarHeight();

            if (Element.IsSet(BarHeightProperty))
            {
                barHeight = Element.OnThisPlatform().GetBarHeight();
            }

            if (barHeight != _lastActionBarHeight && _lastActionBarHeight > 0)
            {
                ResetToolbar();
                bar = _toolbar;
            }
            _lastActionBarHeight = barHeight;

            bar.Measure(MeasureSpecFactory.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly), MeasureSpecFactory.MakeMeasureSpec(barHeight, MeasureSpecMode.Exactly));

            var barOffset       = ToolbarVisible ? barHeight : 0;
            int containerHeight = b - t - ContainerTopPadding - barOffset - ContainerBottomPadding;

            PageController.ContainerArea = new Rectangle(0, 0, Context.FromPixels(r - l), Context.FromPixels(containerHeight));

            // Potential for optimization here, the exact conditions by which you don't need to do this are complex
            // and the cost of doing when it's not needed is moderate to low since the layout will short circuit pretty fast
            Element.ForceLayout();

            bool toolbarLayoutCompleted = false;

            for (var i = 0; i < ChildCount; i++)
            {
                AView child = GetChildAt(i);

                Page childPage = (child as PageContainer)?.Child?.Element as Page;

                if (childPage == null)
                {
                    return;
                }

                // We need to base the layout of both the child and the bar on the presence of the NavBar on the child Page itself.
                // If we layout the bar based on ToolbarVisible, we get a white bar flashing at the top of the screen.
                // If we layout the child based on ToolbarVisible, we get a white bar flashing at the bottom of the screen.
                bool childHasNavBar = NavigationPage.GetHasNavigationBar(childPage);

                if (childHasNavBar)
                {
                    bar.Layout(0, 0, r - l, barHeight);
                    child.Layout(0, barHeight + ContainerTopPadding, r, b - ContainerBottomPadding);
                }
                else
                {
                    bar.Layout(0, -1000, r, barHeight - 1000);
                    child.Layout(0, ContainerTopPadding, r, b - ContainerBottomPadding);
                }
                toolbarLayoutCompleted = true;
            }

            // Making the layout of the toolbar dependant on having a child Page could potentially mean that the toolbar is not laid out.
            // We'll do one more check to make sure it isn't missed.
            if (!toolbarLayoutCompleted)
            {
                if (ToolbarVisible)
                {
                    bar.Layout(0, 0, r - l, barHeight);
                }
                else
                {
                    bar.Layout(0, -1000, r, barHeight - 1000);
                }
            }
        }
Esempio n. 4
0
        void RegisterToolbar()
        {
            Context  context = Context;
            AToolbar bar     = _toolbar;
            Element  page    = Element.RealParent;

            _masterDetailPage = null;
            while (page != null)
            {
                if (page is MasterDetailPage)
                {
                    _masterDetailPage = page as MasterDetailPage;
                    break;
                }
                page = page.RealParent;
            }

            if (_masterDetailPage == null)
            {
                if (PageController.InternalChildren.Count > 0)
                {
                    _masterDetailPage = PageController.InternalChildren[0] as MasterDetailPage;
                }

                if (_masterDetailPage == null)
                {
                    return;
                }
            }

            if (((IMasterDetailPageController)_masterDetailPage).ShouldShowSplitMode)
            {
                return;
            }

            var renderer = Android.Platform.GetRenderer(_masterDetailPage) as MasterDetailPageRenderer;

            if (renderer == null)
            {
                return;
            }

            _drawerLayout = renderer;

            FastRenderers.AutomationPropertiesProvider.GetDrawerAccessibilityResources(context, _masterDetailPage, out int resourceIdOpen, out int resourceIdClose);

            if (_drawerToggle != null)
            {
                _drawerToggle.ToolbarNavigationClickListener = null;
                _drawerToggle.Dispose();
            }

            _drawerToggle = new ActionBarDrawerToggle(context.GetActivity(), _drawerLayout, bar,
                                                      resourceIdOpen == 0 ? global::Android.Resource.String.Ok : resourceIdOpen,
                                                      resourceIdClose == 0 ? global::Android.Resource.String.Ok : resourceIdClose)
            {
                ToolbarNavigationClickListener = new ClickListener(Element)
            };

            if (_drawerListener != null)
            {
                _drawerLayout.RemoveDrawerListener(_drawerListener);
                _drawerListener.Dispose();
            }

            _drawerListener = new DrawerMultiplexedListener {
                Listeners = { _drawerToggle, renderer }
            };
            _drawerLayout.AddDrawerListener(_drawerListener);
        }
Esempio n. 5
0
        void UpdateToolbar()
        {
            if (_disposed)
            {
                return;
            }

            Context  context             = Context;
            AToolbar bar                 = _toolbar;
            ActionBarDrawerToggle toggle = _drawerToggle;

            if (bar == null)
            {
                return;
            }

            bool isNavigated = NavigationPageController.StackDepth > 1;

            bar.NavigationIcon = null;
            Page currentPage = Element.CurrentPage;

            if (isNavigated)
            {
                if (NavigationPage.GetHasBackButton(currentPage) && !Context.IsDesignerContext())
                {
                    if (toggle != null)
                    {
                        toggle.DrawerIndicatorEnabled = false;
                        toggle.SyncState();
                    }

                    var activity = (AppCompatActivity)context.GetActivity();
                    var icon     = new DrawerArrowDrawable(activity.SupportActionBar.ThemedContext);
                    icon.Progress      = 1;
                    bar.NavigationIcon = icon;

                    var prevPage = Element.Peek(1);
                    _defaultNavigationContentDescription = bar.SetNavigationContentDescription(prevPage, _defaultNavigationContentDescription);
                }
                else if (toggle != null && _masterDetailPage != null)
                {
                    toggle.DrawerIndicatorEnabled = _masterDetailPage.ShouldShowToolbarButton();
                    toggle.SyncState();
                }
            }
            else
            {
                if (toggle != null && _masterDetailPage != null)
                {
                    toggle.DrawerIndicatorEnabled = _masterDetailPage.ShouldShowToolbarButton();
                    toggle.SyncState();
                }
            }

            Color tintColor = Element.BarBackgroundColor;

            if (Forms.IsLollipopOrNewer)
            {
                if (tintColor.IsDefault)
                {
                    bar.BackgroundTintMode = null;
                }
                else
                {
                    bar.BackgroundTintMode = PorterDuff.Mode.Src;
                    bar.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid());
                }
            }
            else
            {
                if (tintColor.IsDefault && _backgroundDrawable != null)
                {
                    bar.SetBackground(_backgroundDrawable);
                }
                else if (!tintColor.IsDefault)
                {
                    if (_backgroundDrawable == null)
                    {
                        _backgroundDrawable = bar.Background;
                    }
                    bar.SetBackgroundColor(tintColor.ToAndroid());
                }
            }

            Color textColor = Element.BarTextColor;

            if (!textColor.IsDefault)
            {
                bar.SetTitleTextColor(textColor.ToAndroid().ToArgb());
            }

            Color navIconColor = NavigationPage.GetIconColor(Current);

            if (!navIconColor.IsDefault && bar.NavigationIcon != null)
            {
                DrawableExtensions.SetColorFilter(bar.NavigationIcon, navIconColor, FilterMode.SrcAtop);
            }

            bar.Title = currentPage?.Title ?? string.Empty;

            if (_toolbar.NavigationIcon != null && !textColor.IsDefault)
            {
                var icon = _toolbar.NavigationIcon as DrawerArrowDrawable;
                if (icon != null)
                {
                    icon.Color = textColor.ToAndroid().ToArgb();
                }
            }

            UpdateTitleIcon();

            UpdateTitleView();
        }
Esempio n. 6
0
        protected override void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            _disposed = true;

            if (disposing)
            {
                Device.Info.PropertyChanged -= DeviceInfoPropertyChanged;

                if (NavigationPageController != null)
                {
                    var navController = NavigationPageController;

                    navController.PushRequested             -= OnPushed;
                    navController.PopRequested              -= OnPopped;
                    navController.PopToRootRequested        -= OnPoppedToRoot;
                    navController.InsertPageBeforeRequested -= OnInsertPageBeforeRequested;
                    navController.RemovePageRequested       -= OnRemovePageRequested;
                }

                if (Current != null)
                {
                    Current.PropertyChanged -= CurrentOnPropertyChanged;
                }

                FragmentManager fm = FragmentManager;

                if (!fm.IsDestroyed)
                {
                    FragmentTransaction trans = fm.BeginTransactionEx();
                    foreach (Fragment fragment in _fragmentStack)
                    {
                        trans.RemoveEx(fragment);
                    }
                    trans.CommitAllowingStateLossEx();
                    fm.ExecutePendingTransactionsEx();
                }

                _toolbar.RemoveView(_titleView);
                _titleView?.Dispose();
                _titleView = null;

                if (_titleViewRenderer != null)
                {
                    Android.Platform.ClearRenderer(_titleViewRenderer.View);
                    _titleViewRenderer.Dispose();
                    _titleViewRenderer = null;
                }

                _toolbar.RemoveView(_titleIconView);
                _titleIconView?.Dispose();
                _titleIconView = null;

                _imageSource = null;

                if (_toolbarTracker != null)
                {
                    _toolbarTracker.CollectionChanged -= ToolbarTrackerOnCollectionChanged;

                    _toolbar.DisposeMenuItems(_currentToolbarItems, OnToolbarItemPropertyChanged);

                    _toolbarTracker.Target = null;
                    _toolbarTracker        = null;
                }

                if (_currentMenuItems != null)
                {
                    _currentMenuItems.Clear();
                    _currentMenuItems = null;
                }

                if (_currentToolbarItems != null)
                {
                    _currentToolbarItems.Clear();
                    _currentToolbarItems = null;
                }

                if (_toolbar != null)
                {
                    _toolbar.SetNavigationOnClickListener(null);
                    _toolbar.Menu.Clear();

                    RemoveView(_toolbar);

                    _toolbar.Dispose();
                    _toolbar = null;
                }

                if (_drawerLayout.IsAlive() && _drawerListener.IsAlive())
                {
                    _drawerLayout.RemoveDrawerListener(_drawerListener);

                    RemoveView(_drawerLayout);
                }

                if (_drawerListener != null)
                {
                    _drawerListener.Dispose();
                    _drawerListener = null;
                }

                if (_drawerToggle != null)
                {
                    _drawerToggle.ToolbarNavigationClickListener = null;
                    _drawerToggle.Dispose();
                    _drawerToggle = null;
                }

                if (_backgroundDrawable != null)
                {
                    _backgroundDrawable.Dispose();
                    _backgroundDrawable = null;
                }

                Current = null;

                // We dispose the child renderers after cleaning up everything related to DrawerLayout in case
                // one of the children is a MasterDetailPage (which may dispose of the DrawerLayout).
                if (Element != null)
                {
                    foreach (Element element in PageController.InternalChildren)
                    {
                        var child = element as VisualElement;
                        if (child == null)
                        {
                            continue;
                        }

                        IVisualElementRenderer renderer = Android.Platform.GetRenderer(child);
                        renderer?.Dispose();
                    }
                }
            }

            base.Dispose(disposing);
        }
Esempio n. 7
0
 protected virtual IShellToolbarTracker CreateTrackerForToolbar(Toolbar toolbar)
 {
     return(new ShellToolbarTracker(this, toolbar, ((IShellContext)this).CurrentDrawerLayout));
 }
Esempio n. 8
0
        void UpdateToolbar()
        {
            if (_disposed)
            {
                return;
            }

            Context  context             = Context;
            AToolbar bar                 = _toolbar;
            ActionBarDrawerToggle toggle = _drawerToggle;

            if (bar == null)
            {
                return;
            }

            bool isNavigated = NavigationPageController.StackDepth > 1;

            bar.NavigationIcon = null;
            Page currentPage = Element.CurrentPage;

            if (isNavigated)
            {
                if (NavigationPage.GetHasBackButton(currentPage))
                {
                    if (toggle != null)
                    {
                        toggle.DrawerIndicatorEnabled = false;
                        toggle.SyncState();
                    }

                    var icon = new DrawerArrowDrawable(context.GetThemedContext());
                    icon.Progress      = 1;
                    bar.NavigationIcon = icon;

                    var prevPage        = Element.Peek(1);
                    var backButtonTitle = NavigationPage.GetBackButtonTitle(prevPage);
                    _defaultNavigationContentDescription = backButtonTitle != null
                                                ? bar.SetNavigationContentDescription(prevPage, backButtonTitle)
                                                : bar.SetNavigationContentDescription(prevPage, _defaultNavigationContentDescription);
                }
                else if (toggle != null && _flyoutPage != null)
                {
                    toggle.DrawerIndicatorEnabled = _flyoutPage.ShouldShowToolbarButton();
                    toggle.SyncState();
                }
            }
            else
            {
                if (toggle != null && _flyoutPage != null)
                {
                    toggle.DrawerIndicatorEnabled = _flyoutPage.ShouldShowToolbarButton();
                    toggle.SyncState();
                }
            }

            Color tintColor = Element.BarBackgroundColor;

            if (tintColor == null)
            {
                bar.BackgroundTintMode = null;
            }
            else
            {
                bar.BackgroundTintMode = PorterDuff.Mode.Src;
                bar.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid());
            }

            Brush barBackground = Element.BarBackground;

            bar.UpdateBackground(barBackground);

            Color textColor = Element.BarTextColor;

            if (textColor != null)
            {
                bar.SetTitleTextColor(textColor.ToAndroid().ToArgb());
            }

            Color navIconColor = NavigationPage.GetIconColor(Current);

            if (navIconColor != null && bar.NavigationIcon != null)
            {
                bar.NavigationIcon.SetColorFilter(navIconColor, FilterMode.SrcAtop);
            }

            bar.Title = currentPage?.Title ?? string.Empty;

            if (_toolbar.NavigationIcon != null && textColor != null)
            {
                var icon = _toolbar.NavigationIcon as DrawerArrowDrawable;
                if (icon != null)
                {
                    icon.Color = textColor.ToAndroid().ToArgb();
                }
            }

            UpdateTitleIcon();

            UpdateTitleView();
        }
Esempio n. 9
0
 IShellToolbarTracker IShellContext.CreateTrackerForToolbar(Toolbar toolbar)
 {
     return(CreateTrackerForToolbar(toolbar));
 }
Esempio n. 10
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            var info = FamiStudioForm.Instance != null ? FamiStudioForm.Instance.ActiveDialog as MultiPropertyDialogActivityInfo : null;

            if (savedInstanceState != null || info == null)
            {
                Finish();
                return;
            }

            dlg          = info.Dialog;
            tabsFragment = new MultiPropertyTabFragment(dlg);

            var appBarLayoutParams = new AppBarLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, DroidUtils.GetSizeAttributeInPixel(this, Android.Resource.Attribute.ActionBarSize));

            appBarLayoutParams.ScrollFlags = 0;

            toolbar = new AndroidX.AppCompat.Widget.Toolbar(new ContextThemeWrapper(this, Resource.Style.ToolbarTheme));
            toolbar.LayoutParameters = appBarLayoutParams;
            toolbar.SetTitleTextAppearance(this, Resource.Style.LightGrayTextMediumBold);
            SetSupportActionBar(toolbar);

            ActionBar actionBar = SupportActionBar;

            if (actionBar != null)
            {
                actionBar.SetDisplayHomeAsUpEnabled(true);
                actionBar.SetHomeButtonEnabled(true);
                actionBar.SetHomeAsUpIndicator(Android.Resource.Drawable.IcMenuCloseClearCancel);
                actionBar.Title = dlg.Title;
            }

            appBarLayout = new AppBarLayout(this);
            appBarLayout.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
            appBarLayout.AddView(toolbar);

            var fragmentViewLayoutParams = new NestedScrollView.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);

            fragmentViewLayoutParams.Gravity = GravityFlags.Fill;

            fragmentView = new FragmentContainerView(this);
            fragmentView.LayoutParameters = fragmentViewLayoutParams;
            fragmentView.Id = FragmentViewId;

            var scrollViewLayoutParams = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);

            scrollViewLayoutParams.Behavior = new AppBarLayout.ScrollingViewBehavior(this, null);

            scrollView = new NestedScrollView(new ContextThemeWrapper(this, Resource.Style.DarkBackgroundStyle));
            scrollView.LayoutParameters = scrollViewLayoutParams;
            scrollView.FillViewport     = true;
            scrollView.AddView(fragmentView);

            coordLayout = new CoordinatorLayout(this);
            coordLayout.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
            coordLayout.AddView(appBarLayout);
            coordLayout.AddView(scrollView);

            SetContentView(coordLayout);

            SupportFragmentManager.BeginTransaction()
            .Add(fragmentView.Id, tabsFragment, "MultiPropertyDialogTabs")
            .Commit();
        }
Esempio n. 11
0
        internal static void UpdateMenuItem(
            AToolbar toolbar,
            Context context,
            List <IMenuItem> menuItemsCreated,
            ToolbarItem item,
            Color?tintColor,
            PropertyChangedEventHandler toolbarItemChanged,
            int?menuItemIndex,
            Action <Context, IMenuItem, ToolbarItem> updateMenuItemIcon = null)
        {
            IMenu menu = toolbar.Menu;

            item.PropertyChanged -= toolbarItemChanged;
            item.PropertyChanged += toolbarItemChanged;

            IMenuItem menuitem = null;

            if (menuItemIndex == null)
            {
                menuitem = menu.Add(new Java.Lang.String(item.Text));
                if (menuItemsCreated != null)
                {
                    menuItemsCreated.Add(menuitem);
                }
            }
            else
            {
                if (menuItemsCreated == null || menuItemsCreated.Count < menuItemIndex.Value)
                {
                    return;
                }

                menuitem = menuItemsCreated[menuItemIndex.Value];

                if (!menuitem.IsAlive())
                {
                    return;
                }

                menuitem.SetTitle(new Java.Lang.String(item.Text));
            }

            menuitem.SetEnabled(item.IsEnabled);
            menuitem.SetTitleOrContentDescription(item);

            if (updateMenuItemIcon != null)
            {
                updateMenuItemIcon(context, menuitem, item);
            }
            else
            {
                UpdateMenuItemIcon(context, menu, menuItemsCreated, menuitem, item, tintColor);
            }

            if (item.Order != ToolbarItemOrder.Secondary)
            {
                menuitem.SetShowAsAction(ShowAsAction.Always);
            }

            menuitem.SetOnMenuItemClickListener(new GenericMenuClickListener(((IMenuItemController)item).Activate));

            if (tintColor != null && tintColor != Color.Default)
            {
                var view = toolbar.FindViewById(menuitem.ItemId);
                if (view is ATextView textView)
                {
                    if (item.IsEnabled)
                    {
                        textView.SetTextColor(tintColor.Value.ToAndroid());
                    }
                    else
                    {
                        textView.SetTextColor(tintColor.Value.MultiplyAlpha(0.302).ToAndroid());
                    }
                }
            }
        }
Esempio n. 12
0
        void OnCreate(
            Bundle savedInstanceState,
            ActivationFlags flags)
        {
            Profile.FrameBegin();
            _activityCreated = true;
            if (!AllowFragmentRestore)
            {
                // Remove the automatically persisted fragment structure; we don't need them
                // because we're rebuilding everything from scratch. This saves a bit of memory
                // and prevents loading errors from child fragment managers
                savedInstanceState?.Remove("android:support:fragments");
            }

            Profile.FramePartition("Xamarin.Android.OnCreate");
            base.OnCreate(savedInstanceState);

            Profile.FramePartition("SetSupportActionBar");
            AToolbar bar = null;

            if (ToolbarResource == 0)
            {
                ToolbarResource = Resource.Layout.toolbar;
            }

            if (TabLayoutResource == 0)
            {
                TabLayoutResource = Resource.Layout.tabbar;
            }

            if (ToolbarResource != 0)
            {
                try
                {
                    bar = LayoutInflater.Inflate(ToolbarResource, null).JavaCast <AToolbar>();
                }
                catch (global::Android.Views.InflateException ie)
                {
                    throw new InvalidOperationException("ToolbarResource must be set to a androidx.appcompat.widget.Toolbar", ie);
                }

                if (bar == null)
                {
                    throw new InvalidOperationException("ToolbarResource must be set to a androidx.appcompat.widget.Toolbar");
                }
            }
            else
            {
                bar = new AToolbar(this);
            }

            SetSupportActionBar(bar);

            Profile.FramePartition("SetContentView");
            _layout = new ARelativeLayout(BaseContext);
            SetContentView(_layout);

            Profile.FramePartition("OnStateChanged");
            Microsoft.Maui.Controls.Application.ClearCurrent();

            _previousState = _currentState;
            _currentState  = AndroidApplicationLifecycleState.OnCreate;

            OnStateChanged();

            Profile.FramePartition("Forms.IsLollipopOrNewer");
            if (Forms.IsLollipopOrNewer)
            {
                // Allow for the status bar color to be changed
                if ((flags & ActivationFlags.DisableSetStatusBarColor) == 0)
                {
                    Profile.FramePartition("Set DrawsSysBarBkgrnds");
                    Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
                }
            }
            if (Forms.IsLollipopOrNewer)
            {
                // Listen for the device going into power save mode so we can handle animations being disabled
                Profile.FramePartition("Allocate PowerSaveModeReceiver");
                _powerSaveModeBroadcastReceiver = new PowerSaveModeBroadcastReceiver();
            }

            ContextExtensions.SetDesignerContext(_layout);
            Profile.FrameEnd();
        }
Esempio n. 13
0
 IShellToolbarTracker Microsoft.Maui.Controls.Platform.IShellContext.CreateTrackerForToolbar(Toolbar toolbar)
 {
     return(CreateTrackerForToolbar(toolbar));
 }