Skip to content

Commit

Permalink
Merge pull request #5 from leb-kuchen/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
leb-kuchen authored Apr 19, 2024
2 parents cf224db + 0a38311 commit 0fbe13b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 5 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
[package]
name = "cosmic-applet-apps-menu"
version = "0.1.5"
version = "0.1.6"
edition = "2021"

[dependencies]

i18n-embed = { version = "0.14.1", features = ["fluent-system", "desktop-requester"] }
i18n-embed-fl = "0.8.0"
rust-embed = "8.3.0"


serde = "1.0.197"
i18n-embed = { version = "0.14.1", features = ["fluent-system", "desktop-requester"] }
i18n-embed-fl = "0.8.0"
rust-embed = "8.3.0"
serde = "1.0.197"


cosmic-time = { git = "https://github.com/pop-os/cosmic-time", default-features = false, features = [
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use crate::window::Window;

use config::{AppListConfig, Config, CONFIG_VERSION};
Expand Down
85 changes: 44 additions & 41 deletions src/mouse_area_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ pub struct MouseArea<'a, Message, Theme = cosmic::Theme, Renderer = cosmic::iced
on_right_release: Option<Message>,
on_middle_press: Option<Message>,
on_middle_release: Option<Message>,
on_mouse_wheel: Option<Box<dyn Fn(mouse::ScrollDelta) -> Message + 'a>>,
on_mouse_enter: Option<Message>,
on_enter: Option<Message>,
on_move: Option<Box<dyn Fn(Point) -> Message>>,
on_exit: Option<Message>,
}

impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
Expand Down Expand Up @@ -76,24 +77,34 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
self.on_middle_release = Some(message);
self
}
/// The message to emit when the mouse enters the area.
#[must_use]
/// The message to emit when the mouse wheel is released.
pub fn on_mouse_wheel(mut self, message: impl Fn(mouse::ScrollDelta) -> Message + 'a) -> Self {
self.on_mouse_wheel = Some(Box::new(message));
pub fn on_enter(mut self, message: Message) -> Self {
self.on_enter = Some(message);
self
}

/// The message to emit when the mouse moves in the area.
#[must_use]
pub fn on_move<F>(mut self, build_message: F) -> Self
where
F: Fn(Point) -> Message + 'static,
{
self.on_move = Some(Box::new(build_message));
self
}

/// The message to emit when the mouse exits the area.
#[must_use]
/// The message to emit when the area is hovered
pub fn on_mouse_hover(mut self, message: Message) -> Self {
self.on_mouse_enter = Some(message);
pub fn on_exit(mut self, message: Message) -> Self {
self.on_exit = Some(message);
self
}
}

/// Local state of the [`MouseArea`].
#[derive(Default)]
struct State {
// TODO: Support on_mouse_enter and on_mouse_exit
drag_initiated: Option<Point>,
is_hovered: bool,
}
Expand All @@ -110,8 +121,9 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
on_right_release: None,
on_middle_press: None,
on_middle_release: None,
on_mouse_wheel: None,
on_mouse_enter: None,
on_enter: None,
on_move: None,
on_exit: None,
}
}
}
Expand Down Expand Up @@ -273,16 +285,30 @@ fn update<Message: Clone, Theme, Renderer>(
shell: &mut Shell<'_, Message>,
state: &mut State,
) -> event::Status {
if !cursor.is_over(layout.bounds()) {
if state.is_hovered {
if let Some(_) = widget.on_mouse_enter.as_ref() {
if let Event::Mouse(mouse::Event::CursorMoved { .. }) = event {
state.is_hovered = false;
return event::Status::Captured;
if let Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Event::FingerMoved { .. }) = event
{
let was_hovered = state.is_hovered;
state.is_hovered = cursor.is_over(layout.bounds());

match (
widget.on_enter.as_ref(),
widget.on_move.as_ref(),
widget.on_exit.as_ref(),
) {
(Some(on_enter), _, _) if state.is_hovered && !was_hovered => {
shell.publish(on_enter.clone());
}
(_, Some(on_move), _) if state.is_hovered => {
if let Some(position) = cursor.position_in(layout.bounds()) {
shell.publish(on_move(position));
}
}
(_, _, Some(on_exit)) if !state.is_hovered && was_hovered => {
shell.publish(on_exit.clone());
}
_ => {}
}
return event::Status::Ignored;
}

if let Some(message) = widget.on_press.as_ref() {
Expand Down Expand Up @@ -339,29 +365,6 @@ fn update<Message: Clone, Theme, Renderer>(
}
}

if let Some(message) = widget.on_mouse_wheel.as_ref() {
if let Event::Mouse(mouse::Event::WheelScrolled { delta }) = event {
// todo should scroll x and y be seperate functions or parameters?
// todo threshold, local state, parameter? (pixels laptop scroll)
if let mouse::ScrollDelta::Pixels { y, .. } = delta {
if y.abs() < 5. {
return event::Status::Ignored;
}
}
shell.publish((message)(*delta));
return event::Status::Captured;
}
}
if let Some(message) = widget.on_mouse_enter.as_ref() {
if let Event::Mouse(mouse::Event::CursorMoved { .. }) = event {
if !state.is_hovered {
state.is_hovered = true;
shell.publish(message.clone());
return event::Status::Captured;
}
}
}

if state.drag_initiated.is_none() && widget.on_drag.is_some() {
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) = event
Expand Down
36 changes: 30 additions & 6 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use cosmic::iced_futures::futures::SinkExt;
use cosmic::iced_futures::Subscription;
use cosmic::iced_runtime::core::window;
use cosmic::iced_style::application;
use cosmic::iced_widget::scrollable;
use cosmic::{widget, Apply};
use cosmic::{Element, Theme};
use freedesktop_desktop_entry::DesktopEntry;
Expand Down Expand Up @@ -39,14 +40,15 @@ pub const ID: &str = "dev.dominiccgeh.CosmicAppletAppsMenu";
pub struct Window {
core: Core,
popup: Option<Id>,

config: Config,
app_list_config: AppListConfig,
#[allow(dead_code)]
config_handler: Option<cosmic_config::Config>,
active_category: String,
timeline: Timeline,
entry_map: HashMap<String, Vec<Entry>>,
scrollable_id: widget::Id,
scroll_views: HashMap<String, scrollable::Viewport>,
}

#[derive(Clone, Debug)]
Expand All @@ -59,6 +61,7 @@ pub enum Message {
SpawnExec(String),
Frame(std::time::Instant),
NotifyEvent(notify::Event),
Scroll(scrollable::Viewport),
CategoryUpdate(Option<HashMap<String, Vec<Entry>>>),
}

Expand Down Expand Up @@ -98,11 +101,13 @@ impl cosmic::Application for Window {
core,
config: config.clone(),
config_handler: flags.config_handler,
active_category: "Favorites".into(),
active_category: config.categories.first().cloned().unwrap_or(String::new()),
popup: None,
app_list_config: flags.app_list_config,
entry_map,
timeline: Timeline::new(),
scrollable_id: widget::Id::unique(),
scroll_views: HashMap::new(),
};
(window, update_entry_map(favorites, config))
}
Expand Down Expand Up @@ -175,6 +180,13 @@ impl cosmic::Application for Window {
}
Message::Category(category) => {
self.active_category = category;
return scrollable::scroll_to(
self.scrollable_id.clone(),
match self.scroll_views.get(&self.active_category) {
Some(viewport) => viewport.absolute_offset(),
None => scrollable::AbsoluteOffset::default(),
},
);
}
Message::SpawnExec(exec) => {
cosmic::desktop::spawn_desktop_exec(exec, Vec::<(&str, &str)>::new());
Expand All @@ -198,14 +210,23 @@ impl cosmic::Application for Window {
Message::CategoryUpdate(entry_map) => {
if let Some(entry_map) = entry_map {
self.entry_map = entry_map;
self.scroll_views
.retain(|k, _| self.entry_map.contains_key(k));
}
}
Message::Scroll(viewport) => {
// String leak?
self.scroll_views
.insert(self.active_category.clone(), viewport);
}
}
Command::none()
}

fn view(&self) -> Element<Self::Message> {
cosmic::widget::button(widget::text("Applications").size(14.0))
let padding = self.core.applet.suggested_padding();
widget::button(widget::text("Applications").size(14.0))
.padding([padding / 2, padding])
.style(cosmic::theme::Button::AppletIcon)
.on_press(Message::TogglePopup)
.into()
Expand Down Expand Up @@ -266,8 +287,8 @@ impl cosmic::Application for Window {
if max_category.map_or(true, |max| max != category) {
btn = btn.width(Length::Fill)
}
let area = mouse_area_copy::MouseArea::new(btn)
.on_mouse_hover(Message::Category(category.clone()));
let area =
mouse_area_copy::MouseArea::new(btn).on_enter(Message::Category(category.clone()));
left_side = left_side.push(area).insert_row();
}
let mut right_side = widget::column::with_capacity(active_entries.len());
Expand All @@ -287,7 +308,10 @@ impl cosmic::Application for Window {
let container = widget::container(btn).width(Length::Fill);
right_side = right_side.push(container);
}
let right_scroll = widget::scrollable(right_side).height(500);
let right_scroll = widget::scrollable(right_side)
.height(500)
.id(self.scrollable_id.clone())
.on_scroll(Message::Scroll);

let left_container = widget::container(left_side).width(Length::Shrink);
let right_container = widget::container(right_scroll).width(Length::Fill);
Expand Down

0 comments on commit 0fbe13b

Please sign in to comment.