Skip to content

Commit

Permalink
Common layout widgets (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex authored Dec 16, 2023
1 parent 81c93d6 commit 94f2ec2
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 23 deletions.
13 changes: 9 additions & 4 deletions neothesia/src/scene/menu_scene/iced_menu/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use super::{icons, neo_btn::NeoBtn, Renderer};
use super::{
icons,
layout::{BarLayout, Layout},
neo_btn::NeoBtn,
Renderer,
};
use iced_core::{
alignment::{Horizontal, Vertical},
image::Handle as ImageHandle,
Expand Down Expand Up @@ -251,7 +256,7 @@ impl<'a> Step {
.spacing(40)
.align_items(Alignment::Center);

let mut content = top_padded(column);
let mut layout = Layout::new().body(top_padded(column));

if target.song.is_some() {
let tracks = NeoBtn::new(
Expand Down Expand Up @@ -288,10 +293,10 @@ impl<'a> Step {
left: 0.0,
});

content = content.push(container);
layout = layout.bottom(BarLayout::new().right(container));
}

center_x(content).into()
layout.into()
}
}

Expand Down
29 changes: 10 additions & 19 deletions neothesia/src/scene/menu_scene/iced_menu/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ use iced_widget::{button, column as col, container, row, vertical_space};

use crate::{
iced_utils::iced_state::Element,
scene::menu_scene::{icons, neo_btn::NeoBtn, segment_button, track_card},
scene::menu_scene::{
icons,
layout::{BarLayout, Layout},
neo_btn::NeoBtn,
segment_button, track_card,
},
song::PlayerConfig,
target::Target,
};
Expand Down Expand Up @@ -133,15 +138,6 @@ pub(super) fn view<'a>(_data: &'a Data, target: &Target) -> Element<'a, Message>

let column = iced_widget::scrollable(column);

let mut content = {
let content = column.height(Length::Fill);

col![content]
.width(Length::Fill)
.height(Length::Fill)
.align_items(Alignment::Center)
};

let right = {
let back = NeoBtn::new(
icons::left_arrow_icon()
Expand Down Expand Up @@ -210,13 +206,8 @@ pub(super) fn view<'a>(_data: &'a Data, target: &Target) -> Element<'a, Message>
left: 0.0,
});

let left = row![].width(Length::Fill);
let center = row![center].width(Length::Fill);
let right = row![right].width(Length::Fill);

let main = row![left, center, right].align_items(Alignment::Center);

content = content.push(main);

content.into()
Layout::new()
.body(column)
.bottom(BarLayout::new().center(center).right(right))
.into()
}
141 changes: 141 additions & 0 deletions neothesia/src/scene/menu_scene/layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use crate::iced_utils::iced_state::Element;
use iced_core::{Alignment, Length};
use iced_widget::{column as col, row};

pub struct Layout<'a, Message> {
top: Option<Element<'a, Message>>,
body: Option<Element<'a, Message>>,
bottom: Option<Element<'a, Message>>,
}

impl<'a, M: 'static> Default for Layout<'a, M> {
fn default() -> Self {
Self {
top: None,
body: None,
bottom: None,
}
}
}

impl<'a, M: 'static> Layout<'a, M> {
pub fn new() -> Self {
Self::default()
}

#[allow(unused)]
pub fn top(mut self, top: impl Into<Element<'a, M>>) -> Self {
self.top = Some(top.into());
self
}

pub fn body(mut self, body: impl Into<Element<'a, M>>) -> Self {
self.body = Some(body.into());
self
}

pub fn bottom(mut self, bottom: impl Into<Element<'a, M>>) -> Self {
self.bottom = Some(bottom.into());
self
}

pub fn build(self) -> Element<'a, M> {
let mut root = col![].width(Length::Fill).height(Length::Fill);

if let Some(top) = self.top {
let top = col![top].width(Length::Fill).align_items(Alignment::Center);
root = root.push(top);
}

let body = if let Some(body) = self.body {
col![body]
} else {
col![]
};

let body = col![body]
.width(Length::Fill)
.height(Length::Fill)
.align_items(Alignment::Center);
root = root.push(body);

if let Some(bottom) = self.bottom {
let bottom = col![bottom]
.width(Length::Fill)
.align_items(Alignment::Center);
root = root.push(bottom);
}

root.into()
}
}

impl<'a, M: 'static> From<Layout<'a, M>> for Element<'a, M> {
fn from(val: Layout<'a, M>) -> Self {
val.build()
}
}

pub struct BarLayout<'a, Message> {
left: Option<Element<'a, Message>>,
center: Option<Element<'a, Message>>,
right: Option<Element<'a, Message>>,
}

impl<'a, M: 'static> Default for BarLayout<'a, M> {
fn default() -> Self {
Self {
left: None,
center: None,
right: None,
}
}
}

impl<'a, M: 'static> BarLayout<'a, M> {
pub fn new() -> Self {
Self::default()
}

#[allow(unused)]
pub fn left(mut self, left: impl Into<Element<'a, M>>) -> Self {
self.left = Some(left.into());
self
}

pub fn center(mut self, center: impl Into<Element<'a, M>>) -> Self {
self.center = Some(center.into());
self
}

pub fn right(mut self, right: impl Into<Element<'a, M>>) -> Self {
self.right = Some(right.into());
self
}

pub fn build(self) -> Element<'a, M> {
let mut left = row![].width(Length::Fill);
let mut center = row![].width(Length::Fill);
let mut right = row![].width(Length::Fill);

if let Some(item) = self.left {
left = left.push(item);
}
if let Some(item) = self.center {
center = center.push(item);
}
if let Some(item) = self.right {
right = right.push(item);
}

row![left, center, right]
.align_items(Alignment::Center)
.into()
}
}

impl<'a, M: 'static> From<BarLayout<'a, M>> for Element<'a, M> {
fn from(val: BarLayout<'a, M>) -> Self {
val.build()
}
}
1 change: 1 addition & 0 deletions neothesia/src/scene/menu_scene/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod iced_menu;

mod icons;
mod layout;
mod neo_btn;
mod segment_button;
mod track_card;
Expand Down

0 comments on commit 94f2ec2

Please sign in to comment.