Skip to content

Commit

Permalink
feat(gtk): Improved keyboard navigation
Browse files Browse the repository at this point in the history
- The keyboard may now be used to activate device widget dialogs
- F5 will now execute the scan method
  • Loading branch information
mmstick committed Aug 13, 2019
1 parent ba81fc4 commit 4a56807
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
8 changes: 6 additions & 2 deletions gtk/src/dialogs/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ impl FirmwareUpdateDialog {
upgradeable: bool,
needs_reboot: bool,
) -> Self {
let changelog_entries = &gtk::Box::new(gtk::Orientation::Vertical, 12);
let changelog_entries = &cascade! {
gtk::Box::new(gtk::Orientation::Vertical, 12);
..set_margin_top(6);
..set_margin_bottom(6);
};

changelog.for_each(|(version, entry)| {
let markdown = html2runes::markdown::convert_string(entry.as_ref());
Expand Down Expand Up @@ -83,7 +87,7 @@ impl FirmwareUpdateDialog {
.build()
);
..add(&cascade! {
gtk::Box::new(gtk::Orientation::Vertical, 12);
gtk::Box::new(gtk::Orientation::Vertical, 6);
..add(
&gtk::LabelBuilder::new()
.label(&*header_text)
Expand Down
29 changes: 22 additions & 7 deletions gtk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,25 @@ impl FirmwareWidget {
..set_visible_child(view_empty.as_ref());
};

let container = cascade! {
gtk::Overlay::new();
..add_overlay(&info_bar);
..add(&stack);
..show_all();
let container = {
let sender = sender.clone();
let container = cascade! {
gtk::Overlay::new();
..add_overlay(&info_bar);
..add(&stack);
..show_all();
..set_can_default(true);
..connect_key_press_event(move |_, event| {
gtk::Inhibit(if event.get_keyval() == gdk::enums::key::F5 {
let _ = sender.send(FirmwareEvent::Scan);
true
} else {
false
})
});
};

container
};

info_bar.hide();
Expand Down Expand Up @@ -199,6 +213,7 @@ impl FirmwareWidget {
if let Some(entity) = entity {
let widget = &device_widget_storage[entity];
widget.stack.set_visible_child(&widget.button);
firmware_download_storage.remove(entity);
let _ = tx_progress
.send(ActivateEvent::Deactivate(widget.progress.clone()));
}
Expand Down Expand Up @@ -357,18 +372,18 @@ impl FirmwareWidget {
}

{
// When the device's widget is clicked.
let sender = sender.clone();
let tx_progress = tx_progress.clone();
let stack = widget.stack.downgrade();
let progress = widget.progress.downgrade();
let upgradeable = thelio_io_upgradeable.clone();
let data = thelio_io_upgradeable.clone();
let info = info.clone();
widget.connect_clicked(move || {
let dialog = FirmwareUpdateDialog::new(
info.latest.as_ref(),
iter::once((info.latest.as_ref(), "")),
upgradeable.borrow().upgradeable,
data.borrow().upgradeable,
false,
);

Expand Down
10 changes: 10 additions & 0 deletions gtk/src/views/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@ impl DevicesView {
gtk::ListBox::new();
..set_margin_bottom(12);
..set_selection_mode(gtk::SelectionMode::None);
..connect_row_activated(move |_, row| {
if let Some(widget) = row.get_child() {
let _ = widget.emit("button_press_event", &[&gdk::Event::new(gdk::EventType::ButtonPress)]);
}
});
};

let device_firmware = cascade! {
gtk::ListBox::new();
..set_selection_mode(gtk::SelectionMode::None);
..connect_row_activated(move |_, row| {
if let Some(widget) = row.get_child() {
let _ = widget.emit("button_press_event", &[&gdk::Event::new(gdk::EventType::ButtonPress)]);
}
});
};

let layout: gtk::Box = cascade! {
Expand Down
16 changes: 12 additions & 4 deletions gtk/src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use gtk::prelude::*;
#[derive(Shrinkwrap)]
pub struct DeviceWidget {
#[shrinkwrap(main_field)]
pub container: gtk::EventBox,
pub container: gtk::Container,
pub button: gtk::Button,
pub label: gtk::Label,
pub progress: gtk::ProgressBar,
Expand Down Expand Up @@ -51,7 +51,10 @@ impl DeviceWidget {
};

let container = cascade! {
gtk::EventBox::new();
gtk::EventBoxBuilder::new()
.can_focus(false)
.events(gdk::EventMask::BUTTON_PRESS_MASK)
.build();
..add(&cascade! {
gtk::GridBuilder::new()
.border_width(12)
Expand All @@ -62,10 +65,15 @@ impl DeviceWidget {
..attach(&stack, 1, 0, 1, 2);
});
..show_all();
..set_events(gdk::EventMask::BUTTON_PRESS_MASK);
};

DeviceWidget { container, button, label, progress, stack }
DeviceWidget {
container: container.upcast::<gtk::Container>(),
button,
label,
progress,
stack,
}
}

pub fn connect_clicked<F: Fn() + 'static>(&self, func: F) {
Expand Down

0 comments on commit 4a56807

Please sign in to comment.