Skip to content

Commit

Permalink
refactor(window): mark pick-color as asynchronous function
Browse files Browse the repository at this point in the history
  • Loading branch information
FineFindus committed Jan 13, 2025
1 parent 4ad0b7f commit f97b769
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 49 deletions.
24 changes: 13 additions & 11 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,19 @@ mod imp {

fn command_line(&self, command_line: &gio::ApplicationCommandLine) -> ExitCode {
self.activate();
if command_line.arguments().contains(&"--pick-color".into()) {
self.window.get().unwrap().upgrade().unwrap().pick_color();
if !command_line.arguments().contains(&"--pick-color".into()) {
return glib::ExitCode::SUCCESS;
}

let ctx = glib::MainContext::default();
let window = self.window.get().unwrap().upgrade().unwrap();
ctx.spawn_local(glib::clone!(
#[weak()]
window,
async move {
window.pick_color().await;
}
));
glib::ExitCode::SUCCESS
}

Expand Down Expand Up @@ -138,13 +148,6 @@ impl App {
}

fn setup_gactions(&self) {
// Pick a color using the picker button
let action_pick_color = gio::ActionEntry::builder("pick-color")
.activate(move |app: &Self, _, _| {
app.main_window().pick_color();
})
.build();

// Clear the history
let action_clear_history = gio::ActionEntry::builder("clear-history")
.activate(|app: &Self, _, _| {
Expand Down Expand Up @@ -184,7 +187,6 @@ impl App {
.build();

self.add_action_entries([
action_pick_color,
action_clear_history,
action_random_color,
action_preferences,
Expand All @@ -195,7 +197,7 @@ impl App {

// Sets up keyboard shortcuts
fn setup_accels(&self) {
self.set_accels_for_action("app.pick-color", &["<Control>p"]);
self.set_accels_for_action("win.pick-color", &["<Control>p"]);
self.set_accels_for_action("app.random-color", &["<Control>r"]);
self.set_accels_for_action("app.preferences", &["<Control>comma"]);
self.set_accels_for_action("app.quit", &["<Control>w", "<Control>q"]);
Expand Down
3 changes: 1 addition & 2 deletions src/widgets/history_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ mod imp {
}
}

impl BoxImpl for HistoryItem {}
impl ButtonImpl for HistoryItem {}

#[gtk::template_callbacks]
Expand Down Expand Up @@ -121,7 +120,7 @@ mod imp {

glib::wrapper! {
pub struct HistoryItem(ObjectSubclass<imp::HistoryItem>)
@extends gtk::Box, gtk::Widget, gtk::Button,
@extends gtk::Widget, gtk::Button,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable, gtk::Actionable;
}

Expand Down
66 changes: 30 additions & 36 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ mod imp {
Self::bind_template(klass);
Self::Type::bind_template_callbacks(klass);

klass.install_action("win.pick-color", None, move |win, _, _var| {
win.pick_color();
klass.install_action_async("win.pick-color", None, move |win, _, _var| async move {
win.pick_color().await;
});

klass.install_action(
Expand Down Expand Up @@ -419,7 +419,8 @@ impl AppWindow {
order
.iter()
.filter(|item| visible.contains(item))
.filter_map(|item| Some(ColorFormatRow::new(&Notation::from_str(item).ok()?)))
.flat_map(|item| Notation::from_str(item))
.map(|notation| ColorFormatRow::new(&notation))
.for_each(|widget| {
format_box.append(&widget);
if let Some(color) = self.color() {
Expand All @@ -432,41 +433,34 @@ impl AppWindow {
///
/// It will show a toast when failing to pick a color, for example when the user cancels the action.
#[template_callback]
pub fn pick_color(&self) {
pub async fn pick_color(&self) {
log::debug!("Picking a color using the color picker");
let main_context = glib::MainContext::default();
main_context.spawn_local(glib::clone!(
#[weak(rename_to = window)]
self,
async move {
let root = window.root().expect("Failed to get window root");
let identifier = ashpd::WindowIdentifier::from_native(&root).await;
let request = ashpd::desktop::screenshot::ColorRequest::default()
.identifier(identifier)
.send()
.await;

match request.and_then(|req| req.response()) {
Ok(color) => window.set_color(Color::from(gtk::gdk::RGBA::from(color))),
Err(err) => {
log::error!("{}", err);
if !matches!(
err,
ashpd::Error::Response(ashpd::desktop::ResponseError::Cancelled)
) {
window.show_toast(
gettext("Failed to pick a color"),
adw::ToastPriority::Normal,
);
// show the warning page to indicate to the user that color picking is
// not supported, also disables the color picker to avoid further
// errors
window.imp().show_portal_error_page();
}
}
};
let root = self.root().expect("Failed to get window root");
let identifier = ashpd::WindowIdentifier::from_native(&root).await;
let request = ashpd::desktop::screenshot::ColorRequest::default()
.identifier(identifier)
.send()
.await;

match request.and_then(|req| req.response()) {
Ok(color) => self.set_color(Color::from(gtk::gdk::RGBA::from(color))),
Err(err) => {
log::error!("{}", err);
if !matches!(
err,
ashpd::Error::Response(ashpd::desktop::ResponseError::Cancelled)
) {
self.show_toast(
gettext("Failed to pick a color"),
adw::ToastPriority::Normal,
);
// show the warning page to indicate to the user that color picking is
// not supported, also disables the color picker to avoid further
// errors
self.imp().show_portal_error_page();
}
}
));
};
}

/// Set the current color to the given color.
Expand Down

0 comments on commit f97b769

Please sign in to comment.