Skip to content

Commit 01885b2

Browse files
committed
rabbit-slide: add support for GTK 4
We don't use .ui because: * We can't use the same .ui for GTK 3 and GTK 4 * Glade doesn't support GTK 4 * It seems that Cambalache that is a Glade like tool but it supports both of GTK 3 and GTK 4 isn't stable yet
1 parent fb3bcf8 commit 01885b2

File tree

5 files changed

+272
-313
lines changed

5 files changed

+272
-313
lines changed

lib/rabbit/command/rabbit-slide.rb

Lines changed: 199 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -318,39 +318,64 @@ def available_commands
318318
end
319319

320320
class TextMapper
321-
def initialize(data)
321+
def initialize(data, label_widget, entry_widget)
322322
@data = data
323-
end
324-
325-
def attach(entry)
326-
entry.signal_connect(:notify) do |_widget, param_spec|
327-
if param_spec.name == "text"
328-
if valid?(_widget.text)
329-
_widget.style_context.remove_class(Gtk::STYLE_CLASS_ERROR)
323+
@label_widget = label_widget
324+
@entry_widget = entry_widget
325+
326+
widgets = [@label_widget, @entry_widget]
327+
if Gtk::Version::MAJOR < 4
328+
update_class = lambda do |valid|
329+
if valid
330+
widgets.each do |widget|
331+
widget.style_context.remove_class(Gtk::STYLE_CLASS_ERROR)
332+
end
333+
else
334+
widgets.each do |widget|
335+
widget.style_context.add_class(Gtk::STYLE_CLASS_ERROR)
336+
end
337+
end
338+
end
339+
else
340+
update_class = lambda do |valid|
341+
if valid
342+
widgets.each do |widget|
343+
widget.remove_css_class("error")
344+
end
330345
else
331-
_widget.style_context.add_class(Gtk::STYLE_CLASS_ERROR)
346+
widgets.each do |widget|
347+
widget.add_css_class("error")
348+
end
332349
end
333350
end
334351
end
335-
entry.text = value if value
352+
@entry_widget.signal_connect(:notify) do |_, param_spec|
353+
if param_spec.name == "text"
354+
update_class.call(valid?(@entry_widget.buffer.text))
355+
end
356+
end
357+
if value
358+
@entry_widget.buffer.text = value
359+
else
360+
update_class.call(valid?(@entry_widget.buffer.text))
361+
end
336362
end
337363

338-
def apply(entry)
339-
apply_value(entry.text)
364+
def apply
365+
apply_value(@entry_widget.buffer.text)
340366
end
341367
end
342368

343369
class IntegerMapper
344-
def initialize(data)
370+
def initialize(data, widget)
345371
@data = data
346-
end
372+
@widget = widget
347373

348-
def attach(entry)
349-
entry.value = value if value
374+
@widget.value = value if value
350375
end
351376

352-
def apply(entry)
353-
apply_value(entry.value_as_int)
377+
def apply
378+
apply_value(@widget.value_as_int)
354379
end
355380
end
356381

@@ -385,21 +410,20 @@ def apply_value(value)
385410
end
386411

387412
class SlideMarkupLanguageMapper
388-
def initialize(data)
413+
def initialize(data, widget)
389414
@data = data
415+
@widget = widget
390416
end
391417

392-
def attach(combo_box)
393-
combo_box = combo_box
394-
@data.available_markup_languages.each do |key, value|
395-
combo_box.append(key.to_s, value)
418+
def apply
419+
if Gtk::Version::MAJOR < 4
420+
combo_box = @widget
421+
id = combo_box.active_id
422+
id = id.to_sym if id
423+
else
424+
drop_down = @widget
425+
id = @data.available_markup_languages.keys[drop_down.selected]
396426
end
397-
combo_box.active_id = @data.author_conf.markup_language
398-
end
399-
400-
def apply(combo_box)
401-
id = combo_box.active_id
402-
id = id.to_sym if id
403427
@data.author_conf.markup_language = id
404428
end
405429
end
@@ -426,36 +450,157 @@ def apply_value(value)
426450
end
427451
end
428452

429-
def build_gui_mappers
430-
{
431-
"slide-id" => SlideIDMapper.new(@data),
432-
"slide-base-name" => SlideBaseNameMapper.new(@data),
433-
"slide-markup-language" => SlideMarkupLanguageMapper.new(@data),
434-
"slide-width" => SlideWidthMapper.new(@data),
435-
"slide-height" => SlideHeightMapper.new(@data),
436-
}
437-
end
438-
439453
def show_gui
440454
require_relative "../gtk"
441455

442-
mappers = build_gui_mappers
443-
444-
builder = Gtk::Builder.new(path: File.join(__dir__, "rabbit-slide.ui"))
445-
mappers.each do |id, mapper|
446-
mapper.attach(builder[id])
447-
end
448-
449-
dialog = builder["dialog"]
450-
case dialog.run
451-
when Gtk::ResponseType::CANCEL, Gtk::ResponseType::DELETE_EVENT
452-
false
453-
else
454-
mappers.each do |id, mapper|
455-
mapper.apply(builder[id])
456+
@application = Gtk::Application.new("org.rabbit_shocker.RabbitSlide",
457+
[:non_unique, :handles_command_line])
458+
succeeded = false
459+
@application.signal_connect(:command_line) do |_, command_line|
460+
GLib.application_name = "Rabbit Slide"
461+
@application.activate
462+
succeeded ? 0 : 1
463+
end
464+
@application.signal_connect(:activate) do |_, command_line|
465+
show_window do
466+
successed = true
456467
end
457-
true
458468
end
469+
@application.run
470+
end
471+
472+
def show_window(&on_success)
473+
window = Gtk::ApplicationWindow.new(@application)
474+
475+
grid = Gtk::Grid.new
476+
grid.column_homogeneous = true
477+
nth_row = 0
478+
mappers = []
479+
480+
mappers << add_id_widget(grid, nth_row)
481+
nth_row += 1
482+
483+
mappers << add_base_name_widget(grid, nth_row)
484+
nth_row += 1
485+
486+
mappers << add_markup_language_widget(grid, nth_row)
487+
nth_row += 1
488+
489+
mappers << add_height_widget(grid, nth_row)
490+
nth_row += 1
491+
492+
mappers << add_width_widget(grid, nth_row)
493+
nth_row += 1
494+
495+
nth_column = 0
496+
cancel_button = Gtk::Button.new(mnemonic: _("_Cancel"))
497+
cancel_button.signal_connect(:clicked) do
498+
window.destroy
499+
end
500+
grid.attach(cancel_button, nth_column, nth_row, 1, 1)
501+
nth_column += 1
502+
apply_button = Gtk::Button.new(mnemonic: _("_Apply"))
503+
apply_button.signal_connect(:clicked) do
504+
mappers.each(&:apply)
505+
on_success.call
506+
window.destroy
507+
end
508+
grid.attach(apply_button, nth_column, nth_row, 1, 1)
509+
510+
frame = Gtk::Frame.new(_("Slide information"))
511+
frame.child = grid
512+
window.child = frame
513+
514+
window.show_all if window.respond_to?(:show_all)
515+
window.present
516+
end
517+
518+
def add_id_widget(grid, nth_row)
519+
nth_column = 0
520+
label_widget = Gtk::Label.new(_("ID:"))
521+
label_widget.halign = :end
522+
grid.attach(label_widget,
523+
nth_column, nth_row,
524+
1, 1)
525+
nth_column += 1
526+
entry_widget = Gtk::Entry.new
527+
grid.attach(entry_widget, nth_column, nth_row, 1, 1)
528+
SlideIDMapper.new(@data, label_widget, entry_widget)
529+
end
530+
531+
def add_base_name_widget(grid, nth_row)
532+
nth_column = 0
533+
label_widget = Gtk::Label.new(_("Base name:"))
534+
label_widget.halign = :end
535+
grid.attach(label_widget,
536+
nth_column, nth_row,
537+
1, 1)
538+
nth_column += 1
539+
entry_widget = Gtk::Entry.new
540+
grid.attach(entry_widget, nth_column, nth_row, 1, 1)
541+
SlideBaseNameMapper.new(@data, label_widget, entry_widget)
542+
end
543+
544+
def add_markup_language_widget(grid, nth_row)
545+
nth_column = 0
546+
label_widget = Gtk::Label.new(_("Markup language:"))
547+
label_widget.halign = :end
548+
grid.attach(label_widget,
549+
nth_column, nth_row,
550+
1, 1)
551+
nth_column += 1
552+
if Gtk::Version::MAJOR < 4
553+
widget = Gtk::ComboBoxText.new
554+
@data.available_markup_languages.each do |key, value|
555+
widget.append(key.to_s, value)
556+
end
557+
widget.active_id = @data.author_conf.markup_language
558+
else
559+
markups = @data.available_markup_languages.values
560+
widget = Gtk::DropDown.new(Gtk::StringList.new(markups))
561+
ids = @data.available_markup_languages.keys
562+
widget.selected = ids.index(@data.author_conf.markup_language) || 0
563+
end
564+
grid.attach(widget, nth_column, nth_row, 1, 1)
565+
SlideMarkupLanguageMapper.new(@data, widget)
566+
end
567+
568+
def add_height_widget(grid, nth_row)
569+
nth_column = 0
570+
label_widget = Gtk::Label.new(_("Height:"))
571+
label_widget.halign = :end
572+
grid.attach(label_widget,
573+
nth_column, nth_row,
574+
1, 1)
575+
nth_column += 1
576+
adjustment = Gtk::Adjustment.new(@data.slide_conf.height,
577+
100,
578+
4000,
579+
10,
580+
100,
581+
10)
582+
widget = Gtk::SpinButton.new(adjustment, 10, 0)
583+
grid.attach(widget, nth_column, nth_row, 1, 1)
584+
SlideHeightMapper.new(@data, widget)
585+
end
586+
587+
def add_width_widget(grid, nth_row)
588+
nth_column = 0
589+
label_widget = Gtk::Label.new(_("Width:"))
590+
label_widget.halign = :end
591+
grid.attach(label_widget,
592+
nth_column, nth_row,
593+
1, 1)
594+
nth_column += 1
595+
adjustment = Gtk::Adjustment.new(@data.slide_conf.width,
596+
100,
597+
4000,
598+
10,
599+
100,
600+
10)
601+
widget = Gtk::SpinButton.new(adjustment, 10, 0)
602+
grid.attach(widget, nth_column, nth_row, 1, 1)
603+
SlideWidthMapper.new(@data, widget)
459604
end
460605

461606
def validate

0 commit comments

Comments
 (0)