-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
status line: Custom widget for mouse input handling
- Loading branch information
Showing
6 changed files
with
193 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
use cosmic::{ | ||
iced::{Length, Point, Rectangle}, | ||
iced_native::{ | ||
clipboard::Clipboard, | ||
event::{self, Event}, | ||
layout::{Layout, Limits, Node}, | ||
mouse, | ||
renderer::Style, | ||
touch, | ||
widget::{self, operation::Operation, Tree, Widget}, | ||
Shell, | ||
}, | ||
}; | ||
|
||
use crate::protocol::ClickEvent; | ||
|
||
const BTN_LEFT: u32 = 0x110; | ||
const BTN_RIGHT: u32 = 0x111; | ||
const BTN_MIDDLE: u32 = 0x112; | ||
|
||
/// Wraps a `Row` widget, handling mouse input | ||
pub struct BarWidget<'a, Msg> { | ||
pub row: widget::Row<'a, Msg, cosmic::Renderer>, | ||
pub name_instance: Vec<(Option<&'a str>, Option<&'a str>)>, | ||
pub on_press: fn(ClickEvent) -> Msg, | ||
} | ||
|
||
impl<'a, Msg> Widget<Msg, cosmic::Renderer> for BarWidget<'a, Msg> { | ||
delegate::delegate! { | ||
to self.row { | ||
fn children(&self) -> Vec<Tree>; | ||
fn diff(&self, tree: &mut Tree); | ||
fn layout(&self, renderer: &cosmic::Renderer, limits: &Limits) -> Node; | ||
fn operate( | ||
&self, | ||
tree: &mut Tree, | ||
layout: Layout<'_>, | ||
operation: &mut dyn Operation<Msg>, | ||
); | ||
fn draw( | ||
&self, | ||
state: &Tree, | ||
renderer: &mut cosmic::Renderer, | ||
theme: &cosmic::Theme, | ||
style: &Style, | ||
layout: Layout, | ||
cursor_position: Point, | ||
viewport: &Rectangle, | ||
); | ||
} | ||
} | ||
|
||
fn width(&self) -> Length { | ||
Widget::width(&self.row) | ||
} | ||
|
||
fn height(&self) -> Length { | ||
Widget::height(&self.row) | ||
} | ||
|
||
fn on_event( | ||
&mut self, | ||
tree: &mut Tree, | ||
event: Event, | ||
layout: Layout<'_>, | ||
cursor_position: Point, | ||
renderer: &cosmic::Renderer, | ||
clipboard: &mut dyn Clipboard, | ||
shell: &mut Shell<'_, Msg>, | ||
) -> event::Status { | ||
if self.update(&event, layout, cursor_position, shell) == event::Status::Captured { | ||
return event::Status::Captured; | ||
} | ||
self.row.on_event( | ||
tree, | ||
event, | ||
layout, | ||
cursor_position, | ||
renderer, | ||
clipboard, | ||
shell, | ||
) | ||
} | ||
} | ||
|
||
impl<'a, Msg> From<BarWidget<'a, Msg>> for cosmic::Element<'a, Msg> | ||
where | ||
Msg: 'a, | ||
{ | ||
fn from(widget: BarWidget<'a, Msg>) -> cosmic::Element<'a, Msg> { | ||
cosmic::Element::new(widget) | ||
} | ||
} | ||
|
||
impl<'a, Msg> BarWidget<'a, Msg> { | ||
fn update( | ||
&mut self, | ||
event: &Event, | ||
layout: Layout<'_>, | ||
cursor_position: Point, | ||
shell: &mut Shell<'_, Msg>, | ||
) -> event::Status { | ||
let (button, event_code) = match event { | ||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => (1, BTN_LEFT), | ||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Middle)) => (2, BTN_MIDDLE), | ||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Right)) => (3, BTN_RIGHT), | ||
Event::Touch(touch::Event::FingerPressed { .. }) => (1, BTN_LEFT), | ||
_ => { | ||
return event::Status::Ignored; | ||
} | ||
}; | ||
|
||
let Some((n, bounds)) = layout.children().map(|x| x.bounds()).enumerate().find(|(_, bounds)| bounds.contains(cursor_position)) else { | ||
return event::Status::Ignored; | ||
}; | ||
|
||
let (name, instance) = self.name_instance.get(n).cloned().unwrap_or((None, None)); | ||
|
||
// TODO coordinate space? int conversion? | ||
let x = cursor_position.x as u32; | ||
let y = cursor_position.y as u32; | ||
let relative_x = (cursor_position.x - bounds.x) as u32; | ||
let relative_y = (cursor_position.y - bounds.y) as u32; | ||
let width = bounds.width as u32; | ||
let height = bounds.height as u32; | ||
|
||
shell.publish((self.on_press)(ClickEvent { | ||
name: name.map(str::to_owned), | ||
instance: instance.map(str::to_owned), | ||
x, | ||
y, | ||
button, | ||
event: event_code, | ||
relative_x, | ||
relative_y, | ||
width, | ||
height, | ||
})); | ||
|
||
event::Status::Captured | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters