Skip to content

Commit

Permalink
Move iced widgets to separate crate (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex authored Feb 23, 2024
1 parent c981921 commit 1153bca
Show file tree
Hide file tree
Showing 24 changed files with 206 additions and 180 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

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

24 changes: 17 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[workspace]
members = [
"piano-math",
"wgpu-jumpstart",
"neothesia",
"neothesia-cli",
"neothesia-core",
"midi-file",
"midi-io",
"piano-math",
"wgpu-jumpstart",
"neothesia",
"neothesia-cli",
"neothesia-core",
"midi-file",
"midi-io",
"neothesia-iced-widgets",
]

default-members = ["neothesia"]
Expand All @@ -22,6 +23,15 @@ futures = "0.3"
wgpu-jumpstart = { path = "./wgpu-jumpstart" }
neothesia = { path = "./neothesia", default-features = false }
neothesia-core = { path = "./neothesia-core" }
neothesia-iced-widgets = { path = "./neothesia-iced-widgets" }

midi-file = { path = "./midi-file" }
midi-io = { path = "./midi-io" }
piano-math = { path = "./piano-math" }

iced_style = "0.12"
iced_graphics = "0.12"
iced_core = "0.12"
iced_runtime = "0.12"
iced_wgpu = { version = "0.12", features = ["image"] }
iced_widget = { version = "0.12", features = ["image"] }
12 changes: 6 additions & 6 deletions neothesia-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ edition = "2021"
build-ffmpeg = ["mpeg_encoder/build"]

[dependencies]
neothesia-core = { workspace = true }
midi-file = { workspace = true }
piano-math = { workspace = true }
wgpu-jumpstart = { workspace = true }
env_logger = { workspace = true }
futures = { workspace = true }
neothesia-core.workspace = true
midi-file.workspace = true
piano-math.workspace = true
wgpu-jumpstart.workspace = true
env_logger.workspace = true
futures.workspace = true

mpeg_encoder = { git = "https://github.com/PolyMeilex/mpeg_encoder_next.git" }
14 changes: 7 additions & 7 deletions neothesia-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ edition = "2021"
ron = "0.8"
serde = { version = "1", features = ["serde_derive"] }

log = { workspace = true }
bytemuck = { workspace = true }
wgpu = { workspace = true }
glyphon = { workspace = true }
wgpu-jumpstart = { workspace = true }
piano-math = { workspace = true }
midi-file = { workspace = true }
log.workspace = true
bytemuck.workspace = true
wgpu.workspace = true
glyphon.workspace = true
wgpu-jumpstart.workspace = true
piano-math.workspace = true
midi-file.workspace = true

[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
13 changes: 13 additions & 0 deletions neothesia-iced-widgets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "neothesia-iced-widgets"
version = "0.1.0"
edition = "2021"

[dependencies]
iced_style.workspace = true
iced_graphics.workspace = true
iced_core.workspace = true
iced_wgpu.workspace = true
iced_widget.workspace = true

piano-math = { workspace = true }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::iced_utils::iced_state::Element;
use super::Element;
use iced_core::{Alignment, Length};
use iced_widget::{column as col, row};

Expand Down
20 changes: 20 additions & 0 deletions neothesia-iced-widgets/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub mod layout;
pub mod neo_btn;
pub mod piano_range;
pub mod preferences_group;
pub mod scroll_listener;
pub mod segment_button;
pub mod track_card;
pub mod wrap;

pub use layout::{BarLayout, Layout};
pub use neo_btn::NeoBtn;
pub use piano_range::PianoRange;
pub use preferences_group::{ActionRow, PreferencesGroup};
pub use scroll_listener::ScrollListener;
pub use segment_button::SegmentButton;
pub use track_card::TrackCard;
pub use wrap::Wrap;

type Renderer = iced_wgpu::Renderer;
pub type Element<'a, M> = iced_core::Element<'a, M, iced_style::Theme, iced_wgpu::Renderer>;
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ use iced_graphics::Primitive;
use iced_style::Theme;
use iced_widget::text;

/// Creates a new [`Button`] with the provided content.
pub fn neo_button<'a, Message: Clone>(label: &str) -> NeoBtn<'a, Message> {
NeoBtn::new(
text(label)
.size(30)
.vertical_alignment(Vertical::Center)
.horizontal_alignment(Horizontal::Center),
)
}

pub struct NeoBtn<'a, Message> {
width: Length,
height: Length,
Expand All @@ -35,6 +25,15 @@ pub struct NeoBtn<'a, Message> {
}

impl<'a, Message: Clone> NeoBtn<'a, Message> {
pub fn new_with_label(label: &str) -> Self {
Self::new(
text(label)
.size(30)
.vertical_alignment(Vertical::Center)
.horizontal_alignment(Horizontal::Center),
)
}

pub fn new<E>(content: E) -> Self
where
E: Into<Element<'a, Message, Theme, Renderer>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use iced_core::{
};
use iced_style::Theme;

use crate::iced_utils::iced_state::Element;
use super::Element;

pub struct PianoRange(pub std::ops::RangeInclusive<u8>);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use iced_core::{Font, Length};
use iced_style::Theme;
use iced_widget::{column, container, row, text};

use crate::iced_utils::iced_state::Element;

use super::Renderer;
use super::{Element, Renderer};

mod theme;

Expand All @@ -19,6 +17,12 @@ pub struct PreferencesGroup<'a, MSG> {
items: Vec<Element<'a, MSG>>,
}

impl<'a, MSG: 'a> Default for PreferencesGroup<'a, MSG> {
fn default() -> Self {
Self::new()
}
}

impl<'a, MSG: 'a> PreferencesGroup<'a, MSG> {
pub fn new() -> Self {
Self {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use iced_core::{mouse::ScrollDelta, Length, Size, Widget};
use iced_style::Theme;

use crate::iced_utils::iced_state::Element;
use super::Element;

pub struct ScrollListener<'a, M> {
content: Element<'a, M>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::iced_utils::iced_state::Element;

use super::Renderer;
use super::{Element, Renderer};
use iced_core::{
alignment::{Horizontal, Vertical},
Color, Length,
Expand All @@ -9,30 +7,19 @@ use iced_style::Theme;

mod theme;

fn segment<'a, MSG: 'a>(label: &str) -> iced_widget::Button<'a, MSG, Theme, Renderer> {
iced_widget::button(
iced_widget::text(label)
.horizontal_alignment(Horizontal::Center)
.vertical_alignment(Vertical::Center)
.width(Length::Fill)
.height(Length::Fill),
)
.padding(0)
.width(Length::Fill)
.height(Length::Fill)
}

pub fn segment_button<MSG: Clone>() -> SegmentButton<MSG> {
SegmentButton::new()
}

pub struct SegmentButton<MSG> {
buttons: Vec<(String, MSG)>,
active_color: Color,
active: usize,
}

impl<'a, MSG: Clone + 'a> SegmentButton<MSG> {
impl<M> Default for SegmentButton<M> {
fn default() -> Self {
Self::new()
}
}

impl<MSG> SegmentButton<MSG> {
pub fn new() -> Self {
Self {
buttons: vec![],
Expand All @@ -55,48 +42,64 @@ impl<'a, MSG: Clone + 'a> SegmentButton<MSG> {
self.active_color = color;
self
}
}

fn segment<'a, MSG: 'a>(label: &str) -> iced_widget::Button<'a, MSG, Theme, Renderer> {
iced_widget::button(
iced_widget::text(label)
.horizontal_alignment(Horizontal::Center)
.vertical_alignment(Vertical::Center)
.width(Length::Fill)
.height(Length::Fill),
)
.padding(0)
.width(Length::Fill)
.height(Length::Fill)
}

pub fn build(mut self) -> iced_widget::Container<'a, MSG, Theme, Renderer> {
impl<'a, M: Clone + 'a> From<SegmentButton<M>> for Element<'a, M> {
fn from(mut btn: SegmentButton<M>) -> Self {
let mut new = Vec::new();

let last_id = self.buttons.len() - 1;
let last_id = btn.buttons.len() - 1;

let first = self.buttons.remove(0);
let first = btn.buttons.remove(0);
let first = segment(&first.0)
.style(theme::segment_button(
theme::ButtonSegmentKind::Start,
self.active == 0,
self.active_color,
btn.active == 0,
btn.active_color,
))
.on_press(first.1);

let last = self.buttons.pop().unwrap();
let last = btn.buttons.pop().unwrap();
let last = segment(&last.0)
.style(theme::segment_button(
theme::ButtonSegmentKind::End,
self.active == last_id,
self.active_color,
btn.active == last_id,
btn.active_color,
))
.on_press(last.1);

new.push(first);
for (id, (label, msg)) in self.buttons.into_iter().enumerate() {
for (id, (label, msg)) in btn.buttons.into_iter().enumerate() {
let id = id + 1;
new.push(
segment(&label)
.style(theme::segment_button(
theme::ButtonSegmentKind::Center,
self.active == id,
self.active_color,
btn.active == id,
btn.active_color,
))
.on_press(msg),
);
}
new.push(last);

let new: Vec<Element<MSG>> = new.into_iter().map(|btn| btn.into()).collect();
let new: Vec<Element<M>> = new.into_iter().map(|btn| btn.into()).collect();
iced_widget::container(iced_widget::row(new))
.height(40)
.width(Length::Fill)
.into()
}
}
Loading

0 comments on commit 1153bca

Please sign in to comment.