Skip to content

Commit

Permalink
fix: pass press handler to the intern app button
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Mar 28, 2024
1 parent 8df76d5 commit 8cbd7f8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 36 deletions.
24 changes: 10 additions & 14 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ enum Message {
FinishDndOffer(usize, DesktopEntryData),
LeaveDndOffer,
ScrollYOffset(f32),
Ignore,
GpuUpdate(Option<Vec<Gpu>>),
}

Expand Down Expand Up @@ -580,7 +579,6 @@ impl cosmic::Application for CosmicAppLibrary {
self.group_to_delete = None;
return destroy_layer_surface(DELETE_GROUP_WINDOW_ID.clone());
}
Message::Ignore => {}
Message::FilterApps(input, filtered_apps) => {
self.entry_path_input = filtered_apps;
self.waiting_for_filtered = false;
Expand Down Expand Up @@ -922,27 +920,25 @@ impl cosmic::Application for CosmicAppLibrary {
.iter()
.enumerate()
.map(|(i, entry)| {
let gpu_idx = self.gpus.as_ref().map(|gpus| {
if entry.prefers_dgpu {
gpus.iter().position(|gpu| !gpu.default).unwrap_or(0)
} else {
gpus.iter().position(|gpu| gpu.default).unwrap_or(0)
}
});
let mut b = ApplicationButton::new(
&entry,
Message::Ignore,
move |rect| Message::OpenContextMenu(rect, i),
if self.menu.is_some() {
None
if self.menu.is_none() {
Some(Message::ActivateApp(i, gpu_idx))
} else {
Some(Message::Ignore)
None
},
spacing,
);
if self.menu.is_none() {
let gpu_idx = self.gpus.as_ref().map(|gpus| {
if entry.prefers_dgpu {
gpus.iter().position(|gpu| !gpu.default).unwrap_or(0)
} else {
gpus.iter().position(|gpu| gpu.default).unwrap_or(0)
}
});
b = b
.on_pressed(Message::ActivateApp(i, gpu_idx))
.on_cancel(Message::CancelDrag)
.on_finish(Message::FinishDrag)
.on_create_dnd_source(Message::StartDrag(i))
Expand Down
23 changes: 1 addition & 22 deletions src/widgets/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ pub struct ApplicationButton<'a, Message> {

on_right_release: Box<dyn Fn(Rectangle) -> Message + 'a>,

on_pressed: Option<Message>,

on_create_dnd_source: Option<Message>,

on_finish: Option<Box<dyn Fn(bool) -> Message + 'a>>,
Expand Down Expand Up @@ -68,7 +66,6 @@ impl<'a, Message: Clone + 'static> ApplicationButton<'a, Message> {
path,
..
}: &'a DesktopEntryData,
ignore_button: Message,
on_right_release: impl Fn(Rectangle) -> Message + 'a,
on_pressed: Option<Message>,
spacing: &Spacing,
Expand Down Expand Up @@ -99,7 +96,7 @@ impl<'a, Message: Clone + 'static> ApplicationButton<'a, Message> {
.style(theme::Button::IconVertical)
.padding(spacing.space_s);
let content = if on_pressed.is_some() {
content.on_press(ignore_button.clone())
content.on_press_maybe(on_pressed.clone())
} else {
content
}
Expand All @@ -108,21 +105,13 @@ impl<'a, Message: Clone + 'static> ApplicationButton<'a, Message> {
path: path.clone().unwrap(),
content,
on_right_release: Box::new(on_right_release),
on_pressed,
on_create_dnd_source: None,
on_dnd_command_produced: None,
on_finish: None,
on_cancel: None,
}
}

pub fn on_pressed(self, on_pressed: Message) -> Self {
Self {
on_pressed: Some(on_pressed),
..self
}
}

pub fn on_dnd_command_produced(
self,
message: impl Fn(
Expand Down Expand Up @@ -391,16 +380,6 @@ where
DraggingState::Pressed(start)
}
}
event::Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
| event::Event::Touch(
touch::Event::FingerLifted { .. } | touch::Event::FingerLost { .. },
) => {
ret = event::Status::Captured;
if let Some(on_pressed) = self.on_pressed.clone() {
shell.publish(on_pressed);
}
DraggingState::None
}
_ => DraggingState::Pressed(start),
}
}
Expand Down

0 comments on commit 8cbd7f8

Please sign in to comment.