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

Class Method Details

.alert(parent, message) ⇒ Object



15
16
17
18
# File 'lib/clogs/dialogs.rb', line 15

def alert(parent, message)
  UI::L.msg_box(parent, "", message.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, message, secret: false)
  result = nil
  modal(parent, "", 320, 100) do |win, box, finish|
    UI::L.box_append(box, label(message.to_s), 0)
    entry = secret ? UI::L.new_password_entry : UI::L.new_entry
    UI::L.box_append(box, entry, 0)

    buttons = UI::L.new_horizontal_box
    UI::L.box_set_padded(buttons, 1)
    ok = UI::L.new_button("OK")
    cancel = UI::L.new_button("Cancel")
    UI::L.box_append(buttons, ok, 1)
    UI::L.box_append(buttons, cancel, 1)
    UI::L.box_append(box, buttons, 0)

    UI::L.button_on_clicked(ok) do
      result = UI::L.entry_text(entry).to_s
      finish.call
      0
    end
    UI::L.button_on_clicked(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)
    buttons = UI::L.new_horizontal_box
    UI::L.box_set_padded(buttons, 1)
    yes = UI::L.new_button("OK")
    no = UI::L.new_button("Cancel")
    UI::L.box_append(buttons, yes, 1)
    UI::L.box_append(buttons, no, 1)
    UI::L.box_append(box, buttons, 0)

    UI::L.button_on_clicked(yes) do
      result = true
      finish.call
      0
    end
    UI::L.button_on_clicked(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, message)
  UI::L.msg_box_error(parent, "", message.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

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.

Yields:

  • (win, box, finish)


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

.pointer_to_string(ptr) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/clogs/dialogs.rb', line 41

def pointer_to_string(ptr)
  return nil if ptr.nil? || ptr.null?

  str = ptr.to_s
  UI::L.free_text(ptr)
  str.empty? ? nil : str
end

.save_file(parent) ⇒ Object



30
31
32
33
# File 'lib/clogs/dialogs.rb', line 30

def save_file(parent)
  ptr = UI::L.save_file(parent)
  pointer_to_string(ptr)
end