void UpdateTitle() { if (_toolbar == null || _navigation == null || _titleGroup == null) { return; } var title = GetCurrentPageTitle(); var item = new NSToolbarItem(title); var view = new NSView(); var titleField = new NSTextField { AllowsEditingTextAttributes = true, Bordered = false, DrawsBackground = false, Bezeled = false, Editable = false, Selectable = false, Cell = new VerticallyCenteredTextFieldCell(0f, NSFont.TitleBarFontOfSize(18)), StringValue = title }; titleField.Cell.TextColor = GetTitleColor(); titleField.SizeToFit(); _titleGroup.Group.MinSize = new CGSize(NavigationTitleMinSize, ToolbarHeight); _titleGroup.Group.Subitems = new NSToolbarItem[] { item }; view.AddSubview(titleField); _titleGroup.Group.View = view; //save a reference so we can paint this for the background _nsToolbarItemViewer = _titleGroup.Group.View.Superview; if (_nsToolbarItemViewer == null) { return; } //position is hard .. we manually set the title to be centered var totalWidth = _nsToolbarItemViewer.Superview.Frame.Width; var fieldWidth = titleField.Frame.Width; var x = ((totalWidth - fieldWidth) / 2) - _nsToolbarItemViewer.Frame.X; titleField.Frame = new CGRect(x, 0, fieldWidth, ToolbarHeight); }
void UpdateTitle() { if (_toolbar == null || _navigation == null || _titleGroup == null) { return; } var title = GetCurrentPageTitle(); var item = new NSToolbarItem(title); var view = new NSView(); var titleField = new NSTextField { AllowsEditingTextAttributes = true, Bordered = false, DrawsBackground = false, Bezeled = false, Editable = false, Selectable = false, Cell = new VerticallyCenteredTextFieldCell(0f, NSFont.TitleBarFontOfSize(18)), StringValue = title }; titleField.Cell.TextColor = GetTitleColor(); titleField.SizeToFit(); _titleGroup.Group.MinSize = new CGSize(NavigationTitleMinSize, ToolbarHeight); _titleGroup.Group.Subitems = new NSToolbarItem[] { item }; view.AddSubview(titleField); titleField.CenterXAnchor.ConstraintEqualToAnchor(view.CenterXAnchor).Active = true; titleField.CenterYAnchor.ConstraintEqualToAnchor(view.CenterYAnchor).Active = true; titleField.TranslatesAutoresizingMaskIntoConstraints = false; view.TranslatesAutoresizingMaskIntoConstraints = false; _titleGroup.Group.View = view; //save a reference so we can paint this for the background _nsToolbarItemViewer = _titleGroup.Group.View.Superview; }
private CGRect GetTitleFrame(out NSDictionary titleTextStyles, AppStoreWindow window) { var drawsAsMainWindow = window.DrawsAsMainWindow(); var titleTextShadow = drawsAsMainWindow ? window.TitleTextShadow : window.InactiveTitleTextShadow; if (titleTextShadow == null) { titleTextShadow = new NSShadow { ShadowBlurRadius = 0f, ShadowOffset = new CGSize(0f, -1.0f), ShadowColor = NSColor.FromDeviceWhite(1.0f, 0.5f), }; } var titleTextColor = drawsAsMainWindow ? window.TitleTextColor : window.InactiveTitleTextColor; if (titleTextColor == default(NSColor)) { titleTextColor = AppStoreWindow.DefaultTitleTextColor(drawsAsMainWindow); } var titleFont = window.TitleFont ?? NSFont.TitleBarFontOfSize(NSFont.SystemFontSizeForControlSize(NSControlSize.Regular)); var titleParagraphStyle = (NSParagraphStyle)NSParagraphStyle.DefaultParagraphStyle.MutableCopy();//new NSParagraphStyle { LineBreakMode = NSLineBreakMode.TruncatingTail }; titleParagraphStyle.LineBreakMode = NSLineBreakMode.TruncatingTail; titleTextStyles = NSDictionary.FromObjectsAndKeys( new object[] { titleFont, titleTextColor, titleTextShadow, titleParagraphStyle }, new object[] { NSStringAttributeKey.Font, NSStringAttributeKey.ForegroundColor, NSStringAttributeKey.Shadow, NSStringAttributeKey.ParagraphStyle }); var titleSize = new NSAttributedString(window.Title, titleTextStyles).Size; var titleTextRect = new CGRect(0, 0, titleSize.Width, titleSize.Height); var docIconButton = window.StandardWindowButton(NSWindowButton.DocumentIconButton); var versionButton = window.StandardWindowButton(NSWindowButton.DocumentVersionsButton); var closeButton = window.ButtonToLayout(NSWindowButton.CloseButton); var minimizeButton = window.ButtonToLayout(NSWindowButton.MiniaturizeButton); var zoomButton = window.ButtonToLayout(NSWindowButton.ZoomButton); if (docIconButton != null) { var docIconButtonFrame = ConvertRectFromView(docIconButton.Frame, docIconButton.Superview); titleTextRect.X = docIconButtonFrame.GetMaxX() + TITLE_DOCUMENT_BUTTON_OFFSET.Width; titleTextRect.Y = docIconButtonFrame.GetMidY() - titleSize.Height / 2f + TITLE_DOCUMENT_BUTTON_OFFSET.Height; } else if (versionButton != null) { var versionsButtonFrame = ConvertRectFromView(versionButton.Frame, versionButton.Superview); titleTextRect.X = versionsButtonFrame.GetMinX() - titleSize.Width + TITLE_VERSIONS_BUTTON_OFFSET; var document = ((NSWindowController)window.WindowController).Document; if (document.HasUnautosavedChanges || document.IsDocumentEdited) { titleTextRect.X += TITLE_DOCUMENT_STATUS_X_OFFSET; } } else if (closeButton != null || minimizeButton != null || zoomButton != null) { var closeMaxX = closeButton == null ? 0f : closeButton.Frame.GetMaxX(); var minimizeMaxX = minimizeButton == null ? 0f : minimizeButton.Frame.GetMaxX(); var zoomMaxX = zoomButton == null ? 0f : zoomButton.Frame.GetMaxX(); var adjustedX = NMath.Max(NMath.Max(closeMaxX, minimizeMaxX), zoomMaxX) + TITLE_MARGINS.Width; var proposedX = Bounds.GetMidX() - titleSize.Width / 2f; titleTextRect.X = NMath.Max(proposedX, adjustedX); } else { titleTextRect.X = Bounds.GetMidX() - titleSize.Width / 2f; } var fullScreenButton = window.ButtonToLayout(NSWindowButton.FullScreenButton); if (fullScreenButton != null) { var fullScreenX = fullScreenButton.Frame.X; var maxTitleX = titleTextRect.GetMaxX(); if ((fullScreenX - TITLE_MARGINS.Width) < titleTextRect.GetMaxX()) { titleTextRect.Width = titleTextRect.Size.Width - (maxTitleX - fullScreenX) - TITLE_MARGINS.Width; } } titleTextRect.Y = Bounds.GetMaxY() - titleSize.Height - TITLE_MARGINS.Height; return(titleTextRect); }