Skip to content

Commit

Permalink
Time Picker (#33)
Browse files Browse the repository at this point in the history
* feat: add time picker

* chore: adjust ui

---------

Co-authored-by: ShootingStarDragons <[email protected]>
  • Loading branch information
sundaram123krishnan and Decodetalkers authored Nov 24, 2024
1 parent 844c6b7 commit fd5b0d4
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 16 deletions.
5 changes: 5 additions & 0 deletions lala_bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use zbus_mpirs::ServiceInfo;

use futures::channel::mpsc::Sender;
use iced_aw::date_picker::Date;
use iced_aw::time_picker::Time;
use iced_layershell::reexport::{Anchor, Layer};
use iced_layershell::settings::{LayerShellSettings, Settings, StartMode};
use iced_layershell::to_layer_message;
Expand Down Expand Up @@ -57,6 +58,7 @@ pub enum LaLaInfo {
RightPanel,
ErrorHappened(iced::window::Id),
Calendar,
TimePicker,
}

#[to_layer_message(multi, info_name = "LaLaInfo")]
Expand Down Expand Up @@ -93,6 +95,9 @@ pub enum Message {
ToggleCalendar,
Cancel,
Submit(Date),
ToggleTime,
CancelTime,
SubmitTime(Time),
}

impl From<NotifyMessage> for Message {
Expand Down
151 changes: 135 additions & 16 deletions lala_bar/src/music_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use iced::widget::{
text_input, Space,
};
use iced::{executor, Alignment, Element, Font, Length, Task as Command, Theme};
use iced_aw::{date_picker::Date, helpers::date_picker};
use iced_aw::{date_picker::Date, helpers::date_picker, time_picker, time_picker::Time};
use iced_layershell::reexport::{Anchor, KeyboardInteractivity, Layer, NewLayerShellSettings};
use iced_layershell::MultiApplication;
use iced_runtime::window::Action as WindowAction;
Expand Down Expand Up @@ -54,6 +54,9 @@ pub struct LalaMusicBar {
show_picker: bool,
date: Date,
is_calendar_open: bool,
time: Time,
is_time_picker_open: bool,
time_picker_id: Option<iced::window::Id>,
}

impl LalaMusicBar {
Expand All @@ -64,19 +67,18 @@ impl LalaMusicBar {
let time = self.datetime.time();
let time_info = time.format("%H:%M").to_string();

let week_btn = button(text(week))
let date_btn = button(text(format!("{week} {dateday}")))
.on_press(Message::ToggleCalendar)
.style(button::secondary);
container(row![
week_btn,
Space::with_width(5.),
container(text(time_info)).center_y(Length::Fill),
Space::with_width(5.),
container(text(dateday)).center_y(Length::Fill)
])
.center_y(Length::Fill)
.height(Length::Fill)
.into()

let time_btn = button(text(time_info))
.on_press(Message::ToggleTime)
.style(button::secondary);

container(row![time_btn, Space::with_width(5.), date_btn,])
.center_y(Length::Fill)
.height(Length::Fill)
.into()
}
pub fn update_hidden_notification(&mut self) {
let mut hiddened: Vec<NotifyUnitWidgetInfo> = self
Expand Down Expand Up @@ -462,7 +464,7 @@ impl LalaMusicBar {
sound_slider,
Space::with_width(Length::Fixed(3.)),
self.date_widget(),
Space::with_width(Length::Fixed(3.)),
Space::with_width(Length::Fixed(1.)),
button(text(panel_text)).on_press(Message::ToggleRightPanel)
]
.spacing(10)
Expand Down Expand Up @@ -511,6 +513,9 @@ impl MultiApplication for LalaMusicBar {
show_picker: false,
date: Date::today(),
is_calendar_open: false,
is_time_picker_open: false,
time: Time::now_hm(true),
time_picker_id: None,
},
Command::batch(vec![
Command::done(Message::UpdateBalance),
Expand All @@ -528,6 +533,8 @@ impl MultiApplication for LalaMusicBar {
Some(LaLaInfo::Launcher)
} else if self.hiddenid.is_some_and(|tid| tid == id) {
Some(LaLaInfo::HiddenInfo)
} else if self.time_picker_id.is_some_and(|tid| tid == id) {
Some(LaLaInfo::TimePicker)
} else if self.right_panel.is_some_and(|tid| tid == id) {
Some(LaLaInfo::RightPanel)
} else if self.calendar_id.is_some_and(|tid| tid == id) {
Expand Down Expand Up @@ -561,6 +568,7 @@ impl MultiApplication for LalaMusicBar {
}
LaLaInfo::RightPanel => self.right_panel = Some(id),
LaLaInfo::Calendar => self.calendar_id = Some(id),
LaLaInfo::TimePicker => self.time_picker_id = Some(id),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -602,10 +610,47 @@ impl MultiApplication for LalaMusicBar {
Message::ToggleCalendar => {
if self.is_calendar_open && self.calendar_id.is_some() {
self.is_calendar_open = false;
return iced_runtime::task::effect(Action::Window(WindowAction::Close(
self.calendar_id.unwrap(),
)));
return iced_runtime::task::Task::batch([iced_runtime::task::effect(
Action::Window(WindowAction::Close(self.calendar_id.unwrap())),
)]);
} else if self.is_calendar_open
&& self.is_time_picker_open
&& self.calendar_id.is_some()
&& self.time_picker_id.is_some()
{
self.is_time_picker_open = false;
self.is_calendar_open = false;
return iced_runtime::task::Task::batch([
iced_runtime::task::effect(Action::Window(WindowAction::Close(
self.calendar_id.unwrap(),
))),
iced_runtime::task::effect(Action::Window(WindowAction::Close(
self.time_picker_id.unwrap(),
))),
]);
} else {
if self.is_time_picker_open && self.time_picker_id.is_some() {
self.is_time_picker_open = false;
self.is_calendar_open = true;
return iced_runtime::task::Task::batch([
iced_runtime::task::effect(Action::Window(WindowAction::Close(
self.time_picker_id.unwrap(),
))),
Command::done(Message::NewLayerShell {
settings: NewLayerShellSettings {
size: Some((350, 350)),
exclusive_zone: None,
anchor: Anchor::Right | Anchor::Bottom,
layer: Layer::Top,
margin: Some((10, 10, 10, 10)),
keyboard_interactivity: KeyboardInteractivity::None,
use_last_output: true,
..Default::default()
},
info: LaLaInfo::Calendar,
}),
]);
}
self.is_calendar_open = true;
return Command::done(Message::NewLayerShell {
settings: NewLayerShellSettings {
Expand All @@ -623,6 +668,67 @@ impl MultiApplication for LalaMusicBar {
}
}

Message::ToggleTime => {
if self.is_time_picker_open && self.time_picker_id.is_some() {
self.is_time_picker_open = false;
return iced_runtime::task::Task::batch([iced_runtime::task::effect(
Action::Window(WindowAction::Close(self.time_picker_id.unwrap())),
)]);
} else if self.is_calendar_open
&& self.is_time_picker_open
&& self.calendar_id.is_some()
&& self.time_picker_id.is_some()
{
self.is_time_picker_open = false;
self.is_calendar_open = false;
return iced_runtime::task::Task::batch([
iced_runtime::task::effect(Action::Window(WindowAction::Close(
self.calendar_id.unwrap(),
))),
iced_runtime::task::effect(Action::Window(WindowAction::Close(
self.time_picker_id.unwrap(),
))),
]);
} else {
if self.is_calendar_open && self.calendar_id.is_some() {
self.is_calendar_open = false;
self.is_time_picker_open = true;
return iced_runtime::task::Task::batch([
iced_runtime::task::effect(Action::Window(WindowAction::Close(
self.calendar_id.unwrap(),
))),
Command::done(Message::NewLayerShell {
settings: NewLayerShellSettings {
size: Some((350, 350)),
exclusive_zone: None,
anchor: Anchor::Right | Anchor::Bottom,
layer: Layer::Top,
margin: Some((10, 10, 10, 10)),
keyboard_interactivity: KeyboardInteractivity::None,
use_last_output: true,
..Default::default()
},
info: LaLaInfo::TimePicker,
}),
]);
}
self.is_time_picker_open = true;
return Command::done(Message::NewLayerShell {
settings: NewLayerShellSettings {
size: Some((350, 350)),
exclusive_zone: None,
anchor: Anchor::Right | Anchor::Bottom,
layer: Layer::Top,
margin: Some((10, 10, 10, 10)),
keyboard_interactivity: KeyboardInteractivity::None,
use_last_output: true,
..Default::default()
},
info: LaLaInfo::TimePicker,
});
}
}

Message::Submit(date) => {
self.date = date;
self.show_picker = false;
Expand Down Expand Up @@ -1064,6 +1170,19 @@ impl MultiApplication for LalaMusicBar {
.center_x(Length::Fill)
.into();
}

LaLaInfo::TimePicker => {
return container(time_picker(
true,
self.time,
button(text("Pick time")),
Message::CancelTime,
Message::SubmitTime,
))
.center_y(Length::Fill)
.center_x(Length::Fill)
.into();
}
LaLaInfo::Notify(unitwidgetinfo) => {
let btnwidgets: Element<Message> = unitwidgetinfo.notify_button(self);

Expand Down

0 comments on commit fd5b0d4

Please sign in to comment.