-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Mac implementation for DropDownToolItem
- Add ShowDropArrow for all platforms, and ensure it updates at runtime - Mac has two implementations, one that uses NSSegmentedControl (10.14 and earlier) and another that uses NSMenuToolbarItem (10.15 and later)
- Loading branch information
Showing
17 changed files
with
356 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using Eto.Drawing; | ||
using Eto.Forms; | ||
|
||
namespace Eto.Mac.Forms.ToolBar | ||
{ | ||
public class DropDownToolItemHandler : ToolItemHandler<NSMenuToolbarItem, DropDownToolItem>, DropDownToolItem.IHandler | ||
{ | ||
NSMenu menu; | ||
|
||
protected override bool UseButtonStyle => false; | ||
protected override bool UseAction => false; | ||
|
||
#if MACOS || XAMMAC2 | ||
static readonly IntPtr selInitWithItemIdentifier_Handle = Selector.GetHandle("initWithItemIdentifier:"); | ||
|
||
// constructor with identifier isn't available in the version of macOS workload we need yet.. | ||
class EtoMenuToolbarItem : NSMenuToolbarItem | ||
{ | ||
public EtoMenuToolbarItem() | ||
{ | ||
} | ||
|
||
[Export ("initWithItemIdentifier:")] | ||
public EtoMenuToolbarItem (string itemIdentifier) | ||
: base (NSObjectFlag.Empty) | ||
{ | ||
NSApplication.EnsureUIThread (); | ||
if (itemIdentifier == null) | ||
throw new ArgumentNullException ("itemIdentifier"); | ||
#if USE_CFSTRING | ||
var nsitemIdentifier = CFString.CreateNative(itemIdentifier); | ||
#else | ||
var nsitemIdentifier = NSString.CreateNative(itemIdentifier); | ||
#endif | ||
|
||
if (IsDirectBinding) { | ||
Handle = Messaging.IntPtr_objc_msgSend_IntPtr (this.Handle, selInitWithItemIdentifier_Handle, nsitemIdentifier); | ||
} else { | ||
Handle = Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, selInitWithItemIdentifier_Handle, nsitemIdentifier); | ||
} | ||
NSString.ReleaseNative (nsitemIdentifier); | ||
|
||
} | ||
} | ||
|
||
protected override NSMenuToolbarItem CreateControl() => new EtoMenuToolbarItem(Identifier); | ||
#else | ||
protected override NSMenuToolbarItem CreateControl() => new NSMenuToolbarItem(Identifier); | ||
#endif | ||
|
||
public bool ShowDropArrow | ||
{ | ||
get => Control.ShowsIndicator; | ||
set => Control.ShowsIndicator = value; | ||
} | ||
|
||
protected override void Initialize() | ||
{ | ||
base.Initialize(); | ||
Control.ShowsIndicator = true; | ||
menu = new NSMenu(); | ||
menu.AutoEnablesItems = true; | ||
menu.ShowsStateColumn = true; | ||
Control.Menu = menu; | ||
// first item is never shown, it's the "title" of the pull down?? weird. | ||
menu.InsertItem(new NSMenuItem(string.Empty), 0); | ||
} | ||
|
||
public void AddMenu(int index, MenuItem item) | ||
{ | ||
menu.InsertItem((NSMenuItem)item.ControlObject, index + 1); | ||
} | ||
|
||
public void RemoveMenu(MenuItem item) | ||
{ | ||
menu.RemoveItem((NSMenuItem)item.ControlObject); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
menu.RemoveAllItems(); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/Eto.Mac/Forms/ToolBar/DropDownToolItemPreCatalinaHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using Eto.Drawing; | ||
using Eto.Forms; | ||
using Eto.Mac.Forms.Controls; | ||
using Eto.Mac.Forms.Menu; | ||
|
||
namespace Eto.Mac.Forms.ToolBar | ||
{ | ||
/// <summary> | ||
/// Drop down handler pre-catalina. This could probably be done a little better. | ||
/// </summary> | ||
public class DropDownToolItemPreCatalinaHandler : ToolItemHandler<NSToolbarItem, DropDownToolItem>, DropDownToolItem.IHandler | ||
{ | ||
ContextMenu contextMenu; | ||
MenuSegmentedItem menuItem; | ||
SegmentedButton segmentedButton; | ||
bool showDropArrow = true; | ||
protected override bool UseButtonStyle => false; | ||
|
||
protected override bool IsButton => true; | ||
|
||
protected override bool UseAction => false; | ||
|
||
public bool ShowDropArrow | ||
{ | ||
get => showDropArrow; | ||
set | ||
{ | ||
showDropArrow = value; | ||
if (SegmentedButtonHandler.supportsMenuIndicator) | ||
{ | ||
var nssegmentedControl = SegmentedButtonHandler.GetControl(segmentedButton); | ||
nssegmentedControl.SetShowsMenuIndicator(value, 0); | ||
} | ||
} | ||
} | ||
|
||
protected override void Initialize() | ||
{ | ||
contextMenu = new ContextMenu(); | ||
|
||
menuItem = new MenuSegmentedItem { Menu = contextMenu }; | ||
|
||
segmentedButton = new SegmentedButton(); | ||
segmentedButton.Items.Add(menuItem); | ||
|
||
Control.View = segmentedButton.ToNative(true); | ||
|
||
base.Initialize(); | ||
} | ||
|
||
protected override NSMenuItem CreateMenuFormRepresentation() | ||
{ | ||
var menu = base.CreateMenuFormRepresentation(); | ||
menu.Submenu = contextMenu.ToNS(); | ||
return menu; | ||
} | ||
|
||
public void AddMenu(int index, MenuItem item) | ||
{ | ||
contextMenu.Items.Insert(index, item); | ||
} | ||
|
||
public void RemoveMenu(MenuItem item) | ||
{ | ||
contextMenu.Items.Remove(item); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
contextMenu.Items.Clear(); | ||
} | ||
|
||
protected override void SetImage() | ||
{ | ||
base.SetImage(); | ||
if (Image is Bitmap bmp) | ||
menuItem.Image = bmp.WithSize(ImageSize, ImageSize); | ||
else if (Image is Icon icon) | ||
menuItem.Image = icon.WithSize(ImageSize, ImageSize); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.