Skip to content

Commit

Permalink
[API Breaking] Update menu bar support to require menu_bar keyword …
Browse files Browse the repository at this point in the history
…under `root` or `toplevel` / Support contextual menu by nesting `menu` keyword under `root` or `toplevel`
  • Loading branch information
AndyObtiva committed Dec 5, 2021
1 parent 672c139 commit 3fea6ab
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 0.0.49

- [API Breaking] Update menu bar support to require `menu_bar` keyword under `root` or `toplevel`
- Support contextual menu by nesting `menu` keyword under `root` or `toplevel`

## 0.0.48

- Moved `OS` class to `Glimmer::Tk::OS` to avoid clashing with os gem when installed for users
Expand Down
164 changes: 162 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for Tk 0.0.48
# [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for Tk 0.0.49
## MRI Ruby Desktop Development GUI Library
[![Gem Version](https://badge.fury.io/rb/glimmer-dsl-tk.svg)](http://badge.fury.io/rb/glimmer-dsl-tk)
[![Ruby](https://github.com/AndyObtiva/glimmer-dsl-tk/actions/workflows/ruby.yml/badge.svg)](https://github.com/AndyObtiva/glimmer-dsl-tk/actions/workflows/ruby.yml)
Expand Down Expand Up @@ -125,6 +125,7 @@ Other [Glimmer](https://github.com/AndyObtiva/glimmer) DSL gems you might be int
- [Hello, Scrollbar!](#hello-scrollbar)
- [Hello, Scrollbar Frame!](#hello-scrollbar-frame)
- [Hello, Menu Bar!](#hello-menu-bar)
- [Hello, Contextual Menu!](#hello-contextual-menu)
- [Applications](#applications)
- [Glimmer Tk Calculator](#glimmer-tk-calculator)
- [Y3network Ruby UI](#y3network-ruby-ui)
Expand Down Expand Up @@ -182,7 +183,7 @@ gem install glimmer-dsl-tk

Add the following to `Gemfile`:
```
gem 'glimmer-dsl-tk', '0.0.48'
gem 'glimmer-dsl-tk', '0.0.49'
```

And, then run:
Expand Down Expand Up @@ -310,6 +311,7 @@ keyword(args) | attributes | event bindings & callbacks
`list` | `selectmode`, `selection` | None
`message_box(type: , message: , detail: , title: , icon: , default: , parent: )` | None | None
`menu(label: nil) (label is nil if nested under root/toplevel for menu bar)` | None | None
`menu_bar` | None | None
`menu_item(style = :command, label: , underline: )` (style also can be `:radiobutton`, `:checkbutton`, `:separator`, `:about`, `:preferences`, `:quit`, `:help`) | `state`, `accelerator`, `selection` & `variable` (if `:radiobutton` or `:checkbutton`), `image`, `compound` | `command`
`notebook` | None | None
`radiobutton` | `text`, `variable` (Boolean), `image` (optional keyword args: `subsample`, `zoom`, `from`, `to`, `shrink`, `compositingrule`), `compound` (`'center', 'top', 'bottom', 'left', 'right'`), `value` (default: `text`) | `command {}`
Expand Down Expand Up @@ -3496,6 +3498,164 @@ Glimmer app:

![glimmer dsl tk screenshot sample hello menu-bar](images/glimmer-dsl-tk-screenshot-sample-hello-menu-bar-help.png)

### Hello, Contextual Menu!

Glimmer code (from [samples/hello/hello_contextual_menu.rb](samples/hello/hello_contextual_menu.rb)):

```ruby
require 'glimmer-dsl-tk'

include Glimmer

COLORS = [:white, :red, :yellow, :green, :blue, :magenta, :gray, :black]

Tk::Tile::Style.theme_use 'classic' if OS.mac? # this enables setting background on label just for demo purposes

root { |r|
title 'Hello, Contextual Menu!'

@label = label {
grid row_weight: 1, column_weight: 1
text 'Right-Click To Pop Up Contextual Menu!'
font size: 50
anchor 'center'
}

menu {
menu(label: 'Edit', underline: 0) {
menu_item(label: 'Cut', underline: 2) {
accelerator OS.mac? ? 'Command+X' : 'Control+X'
}

menu_item(label: 'Copy', underline: 0) {
accelerator OS.mac? ? 'Command+C' : 'Control+C'
}

menu_item(label: 'Paste', underline: 0) {
accelerator OS.mac? ? 'Command+V' : 'Control+V'
}
}

menu(label: 'Options', underline: 0) {
menu_item(:checkbutton, label: 'Enabled', underline: 0) {
on('command') do
@select_one_menu.children.each { |menu_item| menu_item.state = menu_item.state == 'disabled' ? 'normal' : 'disabled' }
@select_multiple_menu.children.each { |menu_item| menu_item.state = menu_item.state == 'disabled' ? 'normal' : 'disabled' }
end
}

@select_one_menu = menu(label: 'Select One', underline: 7) {
menu_item(:radiobutton, label: 'Option 1') {
state 'disabled'
}
menu_item(:radiobutton, label: 'Option 2') {
state 'disabled'
}
menu_item(:radiobutton, label: 'Option 3') {
state 'disabled'
}
}

@select_multiple_menu = menu(label: 'Select Multiple', underline: 7) {
menu_item(:checkbutton, label: 'Option 4') {
state 'disabled'
}
menu_item(:checkbutton, label: 'Option 5') {
state 'disabled'
}
menu_item(:checkbutton, label: 'Option 6') {
state 'disabled'
}
}
}

menu(label: 'Language', underline: 3) {
['denmark', 'finland', 'france', 'germany', 'italy', 'mexico', 'netherlands', 'norway', 'usa'].each do |image_name|
menu_item(:radiobutton, label: image_name.capitalize) {
selection image_name == 'usa'
image File.expand_path("images/#{image_name}.png", __dir__)
}
end
}

menu(label: 'Country', underline: 0) {
['denmark', 'finland', 'france', 'germany', 'italy', 'mexico', 'netherlands', 'norway', 'usa'].each do |image_name|
menu_item(:radiobutton, label: image_name.capitalize) {
selection image_name == 'usa'
image File.expand_path("images/#{image_name}.png", __dir__)
compound 'left'
}
end
}

menu(label: 'Format', underline: 3) {
menu(label: 'Background Color', underline: 0) {
COLORS.each { |color_style|
menu_item(:radiobutton, label: color_style.to_s.split('_').map(&:capitalize).join(' ')) {
on('command') do
@label.background = color_style
end
}
}
}

menu(label: 'Foreground Color', underline: 11) {
COLORS.each { |color_style|
menu_item(:radiobutton, label: color_style.to_s.split('_').map(&:capitalize).join(' ')) {
on('command') do
@label.foreground = color_style
end
}
}
}
}

menu(label: 'View', underline: 0) {
menu_item(:radiobutton, label: 'Small', underline: 0) {
accelerator 'Control+S'

on('command') do
@label.font = {size: 25}
end
}

menu_item(:radiobutton, label: 'Medium', underline: 0) {
accelerator 'Control+M'
selection true

on('command') do
@label.font = {size: 50}
end
}

menu_item(:radiobutton, label: 'Large', underline: 0) {
accelerator 'Control+L'

on('command') do
@label.font = {size: 75}
end
}
}
}
}.open
```

Run with [glimmer-dsl-tk](https://rubygems.org/gems/glimmer-dsl-tk) gem installed:

```
ruby -r glimmer-dsl-tk -e "require 'samples/hello/hello_contextual_menu'"
```

Alternatively, run from cloned project without [glimmer-dsl-tk](https://rubygems.org/gems/glimmer-dsl-tk) gem installed:

```
ruby -r ./lib/glimmer-dsl-tk.rb samples/hello/hello_contextual_menu.rb
```

Glimmer app:

![glimmer dsl tk screenshot sample hello contextual-menu](images/glimmer-dsl-tk-screenshot-sample-hello-contextual-menu.png)

## Applications

### Glimmer Tk Calculator
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@
- Consider adding `value` alias for `variable` in `checkbutton` and `radiobutton` (plus any widgets using variable)
- Consider exposing WidgetProxy Tk possible class names (e.g. ::Tk#{SomeWidget}) via Glimmer::Config to make configurable
- Support inter-application Drag and Drop (e.g. drag a file from Finder/Explorer and drop it in Glimmer DSL for Tk app drop zone to store it in a list)
- Consider supporting adding a menu as both contextual and menu_bar
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.48
0.0.49
Binary file modified glimmer-dsl-tk.gemspec
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 12 additions & 3 deletions lib/glimmer/tk/menu_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ def initialize(underscored_widget_name, parent_proxy, args, &block)
def post_add_content
case @parent_proxy
when ToplevelProxy
@parent_proxy.tk['menu'] = @tk
if @keyword == 'menu_bar'
@parent_proxy.tk['menu'] = @tk
else
if OS.mac?
@parent_proxy.tk.bind '2', proc{|x,y| tk.popup(x,y)}, "%X %Y"
@parent_proxy.tk.bind 'Control-1', proc{|x,y| tk.popup(x,y)}, "%X %Y"
else
@parent_proxy.tk.bind '3', proc{|x,y| tk.popup(x,y)}, "%X %Y"
end
end
end
end

Expand Down Expand Up @@ -75,8 +84,7 @@ def build_widget
# elsif @parent_proxy.parent_proxy.is_a?(ToplevelProxy) && OS.windows? && system?
# @tk = ::TkSysMenu_System.new(@parent_proxy.tk).tap {|tk| tk.singleton_class.include(Glimmer::Tk::Widget); tk.proxy = self}
else
tk_widget_class = self.class.tk_widget_class_for(@keyword)
@tk = tk_widget_class.new(@parent_proxy.tk).tap {|tk| tk.singleton_class.include(Glimmer::Tk::Widget); tk.proxy = self}
@tk = ::Tk::Menu.new(@parent_proxy.tk).tap {|tk| tk.singleton_class.include(Glimmer::Tk::Widget); tk.proxy = self}
end
case @parent_proxy
when MenuProxy
Expand All @@ -85,5 +93,6 @@ def build_widget
end
end
end
MenuBarProxy = MenuProxy
end
end

0 comments on commit 3fea6ab

Please sign in to comment.