Skip to content

Commit

Permalink
feat: make row clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Feb 13, 2024
1 parent c8410a4 commit 9adb72b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/app/entries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::app::style::rows::button::ButtonStyle;
use crate::app::style::rows::icon::IconStyle;
use crate::app::style::rows::RowStyles;
use crate::app::Message;
use crate::icons::{fallback_icon, Extension, IconPath};
use crate::THEME;
use iced::widget::{column, container, row, text, Container, Image, Row};
use iced::widget::{column, container, row, text, Button, Container, Image, Row};
use iced::{Alignment, Length, Renderer};
use std::borrow::Cow;

Expand Down Expand Up @@ -35,14 +36,19 @@ pub(crate) trait AsEntry<'a> {
// See : https://github.com/iced-rs/iced/pull/1044
.align_items(Alignment::Start);

self.as_row(row, theme)
self.as_row(row, theme, idx)
}

fn as_row<'b>(&self, row: Row<'b, Message>, theme: &'static RowStyles) -> Container<'b, Message>
fn as_row<'b>(
&self,
row: Row<'b, Message>,
theme: &'static RowStyles,
idx: usize,
) -> Container<'b, Message>
where
'b: 'a,
{
let title_row: iced::widget::Container<'_, Message, Renderer> =
let title_row: Container<'_, Message, Renderer> =
container(iced::widget::row(vec![text(self.get_display_name())
.size(theme.title.font_size)
.into()]))
Expand Down Expand Up @@ -73,7 +79,11 @@ pub(crate) trait AsEntry<'a> {
_ => column,
};

Container::new(row.push(column))
let button = Button::new(row.push(column))
.style(iced::theme::Button::Custom(Box::new(&ButtonStyle)))
.on_press(Message::Click(idx));

Container::new(button)
.style(iced::theme::Container::Custom(Box::new(theme)))
.padding(theme.padding.to_iced_padding())
.width(theme.width)
Expand Down Expand Up @@ -123,8 +133,8 @@ pub(crate) trait AsEntry<'a> {
Extension::Svg => Container::new(
icon.as_ref()
.to_svg(&theme.color)
.height(iced_core::Length::Fixed(theme.icon_size as f32))
.width(iced_core::Length::Fixed(theme.icon_size as f32)),
.height(Length::Fixed(theme.icon_size as f32))
.width(Length::Fixed(theme.icon_size as f32)),
),
Extension::Png => Container::new(
Image::new(&icon.as_ref().path)
Expand Down
9 changes: 9 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub struct Onagre<'a> {
pub enum Message {
Loading,
InputChanged(String),
Click(usize),
KeyboardEvent(KeyCode),
SubscriptionResponse(SubscriptionMessage),
Unfocused,
Expand Down Expand Up @@ -136,6 +137,14 @@ impl Application for Onagre<'_> {
Command::none()
}
}
Message::Click(row_idx) => {
match self.state.get_active_mode() {
ActiveMode::History => self.state.selected = Selection::History(row_idx),
_ => self.state.selected = Selection::PopLauncher(row_idx),
}

self.on_execute()
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions src/app/style/rows/button.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use iced_core::{Color, Vector};
use iced_style::button::{Appearance, StyleSheet};

// Button is just used as a wrapper to get access to the click event.
// For now all theming option is disabled, we might want to make
// on hovered theming options available in the config later.
pub struct ButtonStyle;

impl StyleSheet for &ButtonStyle {
type Style = iced::Theme;

fn active(&self, _: &Self::Style) -> Appearance {
no_style()
}

fn hovered(&self, _: &Self::Style) -> Appearance {
no_style()
}

fn pressed(&self, _: &Self::Style) -> Appearance {
no_style()
}

fn disabled(&self, _: &Self::Style) -> Appearance {
no_style()
}
}

fn no_style() -> Appearance {
Appearance {
shadow_offset: Vector { x: 0.0, y: 0.0 },
background: None,
border_radius: 0.0.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
text_color: Color::BLACK,
}
}
1 change: 1 addition & 0 deletions src/app/style/rows/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl Scale for GenericContainerStyle {
self
}
}

impl Default for GenericContainerStyle {
fn default() -> Self {
GenericContainerStyle {
Expand Down
1 change: 1 addition & 0 deletions src/app/style/rows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use iced_core::BorderRadius;
use iced_style::container::{Appearance, StyleSheet};
use icon::IconStyle;

pub mod button;
pub mod generic;
pub mod icon;

Expand Down

0 comments on commit 9adb72b

Please sign in to comment.