Module: Clogs::Dialogs
- Defined in:
- lib/clogs/dialogs.rb
Overview
Shoes' modal helpers.
libui gives us exactly two dialogs of its own -- a message box and a file
picker -- and neither can ask a question. ask and confirm are therefore
built as small real windows made of native libui controls, run on a nested
uiMainStep loop so they block the caller the way Shoes expects.
Class Method Summary collapse
- .alert(parent, message) ⇒ Object
-
.ask(parent, message, secret: false) ⇒ Object
A one-line text prompt.
-
.confirm(parent, question) ⇒ Object
A yes/no question.
- .error(parent, message) ⇒ Object
- .label(text) ⇒ Object
-
.modal(parent, title, width, height) {|win, box, finish| ... } ⇒ Object
Run a modal window on a nested event loop.
- .open_file(parent) ⇒ Object
- .open_folder(parent) ⇒ Object
- .pointer_to_string(ptr) ⇒ Object
- .save_file(parent) ⇒ Object
Class Method Details
.alert(parent, message) ⇒ Object
15 16 17 18 |
# File 'lib/clogs/dialogs.rb', line 15 def alert(parent, ) UI::L.msg_box(parent, "", .to_s) nil end |
.ask(parent, message, secret: false) ⇒ Object
A one-line text prompt. Returns the string, or nil if cancelled.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/clogs/dialogs.rb', line 50 def ask(parent, , secret: false) result = nil modal(parent, "", 320, 100) do |win, box, finish| UI::L.box_append(box, label(.to_s), 0) entry = secret ? UI::L.new_password_entry : UI::L.new_entry UI::L.box_append(box, entry, 0) = UI::L.new_horizontal_box UI::L.box_set_padded(, 1) ok = UI::L.("OK") cancel = UI::L.("Cancel") UI::L.box_append(, ok, 1) UI::L.box_append(, cancel, 1) UI::L.box_append(box, , 0) UI::L.(ok) do result = UI::L.entry_text(entry).to_s finish.call 0 end UI::L.(cancel) do result = nil finish.call 0 end _ = win end result end |
.confirm(parent, question) ⇒ Object
A yes/no question. Returns true or false.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/clogs/dialogs.rb', line 81 def confirm(parent, question) result = false modal(parent, "", 320, 90) do |_win, box, finish| UI::L.box_append(box, label(question.to_s), 0) = UI::L.new_horizontal_box UI::L.box_set_padded(, 1) yes = UI::L.("OK") no = UI::L.("Cancel") UI::L.box_append(, yes, 1) UI::L.box_append(, no, 1) UI::L.box_append(box, , 0) UI::L.(yes) do result = true finish.call 0 end UI::L.(no) do result = false finish.call 0 end end result end |
.error(parent, message) ⇒ Object
20 21 22 23 |
# File 'lib/clogs/dialogs.rb', line 20 def error(parent, ) UI::L.msg_box_error(parent, "", .to_s) nil end |
.label(text) ⇒ Object
107 108 109 |
# File 'lib/clogs/dialogs.rb', line 107 def label(text) UI::L.new_label(text) end |
.modal(parent, title, width, height) {|win, box, finish| ... } ⇒ Object
Run a modal window on a nested event loop. libui does not allow uiMain to be re-entered, but stepping it by hand is fine and is how a blocking dialog has to work here.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/clogs/dialogs.rb', line 114 def modal(parent, title, width, height) win = UI::L.new_window(title, width, height, 0) UI::L.window_set_margined(win, 1) box = UI::L.new_vertical_box UI::L.box_set_padded(box, 1) UI::L.window_set_child(win, box) done = false finish = -> { done = true } UI::L.window_on_closing(win) do done = true 0 end yield win, box, finish UI::L.control_show(win) UI::L.main_step(1) until done UI::L.control_destroy(win) _ = parent end |
.open_file(parent) ⇒ Object
25 26 27 28 |
# File 'lib/clogs/dialogs.rb', line 25 def open_file(parent) ptr = UI::L.open_file(parent) pointer_to_string(ptr) end |
.open_folder(parent) ⇒ Object
35 36 37 38 39 |
# File 'lib/clogs/dialogs.rb', line 35 def open_folder(parent) return nil unless UI::L.respond_to?(:open_folder) pointer_to_string(UI::L.open_folder(parent)) end |