-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from demarey/menu-support
add an API to easily build Mac Os menus
- Loading branch information
Showing
33 changed files
with
1,057 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
" | ||
I represent an item of a Cocoa Menu. | ||
I can hold items or submenus. | ||
" | ||
Class { | ||
#name : #CocoaMenu, | ||
#superclass : #Object, | ||
#instVars : [ | ||
'nsMenu', | ||
'items', | ||
'title', | ||
'nsTitle', | ||
'menuItem' | ||
], | ||
#classVars : [ | ||
'MainMenu' | ||
], | ||
#category : #'ObjectiveC-Cocoa-Menus' | ||
} | ||
|
||
{ #category : #'instance creation' } | ||
CocoaMenu class >> with: aNSMenu [ | ||
|
||
^ self new | ||
setMenu: aNSMenu; | ||
yourself | ||
] | ||
|
||
{ #category : #adding } | ||
CocoaMenu >> addItemWithTitle: aString action: aBlock [ | ||
|
||
^ self addItemWithTitle: aString action: aBlock shortcut: '' | ||
] | ||
|
||
{ #category : #adding } | ||
CocoaMenu >> addItemWithTitle: aTitle action: actionBlock shortcut: shortcutString [ | ||
|
||
items add: (CocoaMenuItem new | ||
title: aTitle; | ||
action: actionBlock; | ||
shortcut: shortcutString; | ||
yourself) | ||
] | ||
|
||
{ #category : #adding } | ||
CocoaMenu >> addSeparator [ | ||
|
||
items add: CocoaMenuSeparator new | ||
] | ||
|
||
{ #category : #adding } | ||
CocoaMenu >> addServicesMenu [ | ||
|
||
items add: CocoaServicesMenu new | ||
] | ||
|
||
{ #category : #adding } | ||
CocoaMenu >> addSubmenu: aTitle with: builderBlock [ | ||
|
||
| menu | | ||
menu := self class new. | ||
menu title: aTitle. | ||
builderBlock value: menu. | ||
items add: menu. | ||
^ menu | ||
] | ||
|
||
{ #category : #private } | ||
CocoaMenu >> addToMenu: aCocoaMenu [ | ||
<ignoreNotImplementedSelectors: #(#addItemWithTitle:action:keyEquivalent: #setSubmenu:forItem:)> | ||
|
||
self buildNSMenu. | ||
menuItem := aCocoaMenu nsMenu | ||
addItemWithTitle: nsTitle | ||
action: ObjCObject nil | ||
keyEquivalent: '' asNSString. | ||
|
||
aCocoaMenu nsMenu setSubmenu: nsMenu forItem: menuItem. | ||
|
||
] | ||
|
||
{ #category : #configuring } | ||
CocoaMenu >> beMainMenu [ | ||
|
||
self buildNSMenu. | ||
#NSApplication inObjC sharedApplication setMainMenu: nsMenu. | ||
MainMenu := self | ||
] | ||
|
||
{ #category : #private } | ||
CocoaMenu >> buildNSMenu [ | ||
<ignoreNotImplementedSelectors: #(#initWithTitle:)> | ||
|
||
nsTitle := (title ifNil: [ '' ]) asNSString. | ||
nsMenu := #NSMenu inObjC alloc initWithTitle: nsTitle. | ||
items do: [ :item | item addToMenu: self ]. | ||
^ nsMenu | ||
] | ||
|
||
{ #category : #initialization } | ||
CocoaMenu >> initialize [ | ||
|
||
super initialize. | ||
items := OrderedCollection new | ||
] | ||
|
||
{ #category : #accessing } | ||
CocoaMenu >> nsMenu [ | ||
|
||
^ nsMenu | ||
] | ||
|
||
{ #category : #printing } | ||
CocoaMenu >> printOn: aStream [ | ||
aStream << self class name << '(' << (title ifNil: [ '' ]) << ')' | ||
] | ||
|
||
{ #category : #removing } | ||
CocoaMenu >> removeAllItems [ | ||
|
||
nsMenu removeAllItems. | ||
|
||
self flag: 'TODO: release ObjC menu items'. | ||
items := OrderedCollection new. | ||
|
||
] | ||
|
||
{ #category : #private } | ||
CocoaMenu >> setMenu: aNSMenu [ | ||
|
||
nsMenu := aNSMenu | ||
] | ||
|
||
{ #category : #accessing } | ||
CocoaMenu >> title: aString [ | ||
|
||
title := aString | ||
] |
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,57 @@ | ||
" | ||
I represent an item of a Cocoa menu. | ||
I can hold menu items or submenus. | ||
" | ||
Class { | ||
#name : #CocoaMenuItem, | ||
#superclass : #Object, | ||
#instVars : [ | ||
'action', | ||
'shortcut', | ||
'title', | ||
'target', | ||
'nsTitle', | ||
'nsShortcut', | ||
'menuItem' | ||
], | ||
#category : #'ObjectiveC-Cocoa-Menus' | ||
} | ||
|
||
{ #category : #accessing } | ||
CocoaMenuItem >> action: aBlock [ | ||
|
||
action := aBlock | ||
] | ||
|
||
{ #category : #adding } | ||
CocoaMenuItem >> addToMenu: aCocoaMenu [ | ||
<ignoreNotImplementedSelectors: #(#addItemWithTitle:action:keyEquivalent: #setTarget: #setEnabled:)> | ||
|
||
nsTitle := title asNSString. | ||
nsShortcut := shortcut asNSString. | ||
|
||
menuItem := aCocoaMenu nsMenu | ||
addItemWithTitle: nsTitle | ||
action: #execute asObjCSelector | ||
keyEquivalent: nsShortcut. | ||
|
||
target := CocoaMenuTarget new | ||
block: action; | ||
yourself. | ||
ObjCProxyClass newFor: target. | ||
menuItem setTarget: target. | ||
menuItem setEnabled: true | ||
|
||
] | ||
|
||
{ #category : #accessing } | ||
CocoaMenuItem >> shortcut: aString [ | ||
|
||
shortcut := aString | ||
] | ||
|
||
{ #category : #accessing } | ||
CocoaMenuItem >> title: aString [ | ||
|
||
title := aString | ||
] |
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,17 @@ | ||
" | ||
A Cocoa menu separator. I'm also a menu item. | ||
" | ||
Class { | ||
#name : #CocoaMenuSeparator, | ||
#superclass : #Object, | ||
#category : #'ObjectiveC-Cocoa-Menus' | ||
} | ||
|
||
{ #category : #adding } | ||
CocoaMenuSeparator >> addToMenu: aCocoaMenu [ | ||
<ignoreNotImplementedSelectors: #(#separatorItem)> | ||
|
||
| separator | | ||
separator := #NSMenuItem inObjC separatorItem. | ||
aCocoaMenu nsMenu addItem: separator | ||
] |
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,43 @@ | ||
" | ||
I represent a Cocoa menu target, i.e. the ObjectiveC object that will receive the callbacks. | ||
" | ||
Class { | ||
#name : #CocoaMenuTarget, | ||
#superclass : #Object, | ||
#instVars : [ | ||
'proxy', | ||
'block' | ||
], | ||
#category : #'ObjectiveC-Cocoa-Menus' | ||
} | ||
|
||
{ #category : #accessing } | ||
CocoaMenuTarget >> block [ | ||
|
||
^ block | ||
] | ||
|
||
{ #category : #accessing } | ||
CocoaMenuTarget >> block: anObject [ | ||
|
||
block := anObject | ||
] | ||
|
||
{ #category : #execution } | ||
CocoaMenuTarget >> execute [ | ||
<objCSignature: #(void ())> | ||
|
||
block value | ||
] | ||
|
||
{ #category : #accessing } | ||
CocoaMenuTarget >> proxy [ | ||
|
||
^ proxy | ||
] | ||
|
||
{ #category : #accessing } | ||
CocoaMenuTarget >> proxy: anObject [ | ||
|
||
proxy := anObject | ||
] |
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,42 @@ | ||
" | ||
I represent the Mac os ""Services"" menu. | ||
You can use me to add a ""Services"" entry to your app menu. | ||
" | ||
Class { | ||
#name : #CocoaServicesMenu, | ||
#superclass : #CocoaMenu, | ||
#category : #'ObjectiveC-Cocoa-Menus' | ||
} | ||
|
||
{ #category : #configuring } | ||
CocoaServicesMenu >> beServicesMenu [ | ||
<ignoreNotImplementedSelectors: #(#setServicesMenu:)> | ||
|
||
#NSApplication inObjC sharedApplication setServicesMenu: nsMenu | ||
] | ||
|
||
{ #category : #private } | ||
CocoaServicesMenu >> buildNSMenu [ | ||
|
||
nsMenu := self servicesMenu. | ||
nsMenu isNull | ||
ifTrue: [ | ||
super buildNSMenu. | ||
self beServicesMenu ] | ||
ifFalse: [ nsTitle := title asNSString ]. | ||
^ nsMenu | ||
] | ||
|
||
{ #category : #initialization } | ||
CocoaServicesMenu >> initialize [ | ||
|
||
super initialize. | ||
title := 'Services' | ||
] | ||
|
||
{ #category : #accessing } | ||
CocoaServicesMenu >> servicesMenu [ | ||
"Get the Application services menu if set or null" | ||
|
||
^ #NSApplication inObjC sharedApplication servicesMenu | ||
] |
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
Oops, something went wrong.