Skip to content

Commit

Permalink
Updated wrap, attempted to update tab_bar
Browse files Browse the repository at this point in the history
  • Loading branch information
genusistimelord committed Jul 25, 2023
1 parent 43e9667 commit 80a3d30
Show file tree
Hide file tree
Showing 11 changed files with 400 additions and 207 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Selection List now will clear Selected if a new Item is added to the same ID location as the last and its Hash is different.
- Manual Override Will always be used over the internal selected if set to Some(). if the # doesnt Exist it Defaults Selected to None.
- Added Helper functions for Widgets.

### Changed
- Breaking Selection List now Takes in Font and Manual Selected override on new_with.
Expand Down
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ default = [
"icon_text",
"grid",
"modal",
#"tab_bar",
"tab_bar",
#"tabs",
#"time_picker",
#"wrap",
"wrap",
"selection_list",
"split",
#"menu",
#"quad",
"quad",
#"context_menu"
]

Expand Down Expand Up @@ -98,10 +98,10 @@ members = [
"examples/grid",
"examples/modal",
"examples/modal_component",
#"examples/tab_bar",
"examples/tab_bar",
#"examples/tabs",
#"examples/time_picker",
#"examples/wrap",
"examples/wrap",
"examples/number_input",
"examples/selection_list",
"examples/split",
Expand Down
208 changes: 131 additions & 77 deletions examples/tab_bar/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use iced::{
widget::{Button, Column, Row, Text, TextInput},
Alignment, Element, Length, Sandbox, Settings,
alignment, font,
widget::{container, text, Button, Column, Row, Text, TextInput},
Alignment, Application, Command, Element, Length, Settings, Theme,
};
use iced_aw::{TabBar, TabLabel};

Expand All @@ -15,111 +16,164 @@ enum Message {
TabLabelInputChanged(String),
TabContentInputChanged(String),
NewTab,
Loaded(Result<(), String>),
FontLoaded(Result<(), font::Error>),
}

struct TabBarExample {
#[derive(Debug)]
enum TabBarExample {
Loading,
Loaded(State),
}

#[derive(Debug)]
struct State {
active_tab: usize,
new_tab_label: String,
new_tab_content: String,
tabs: Vec<(String, String)>,
}

impl Sandbox for TabBarExample {
async fn load() -> Result<(), String> {
Ok(())
}

impl Application for TabBarExample {
type Message = Message;
type Theme = Theme;
type Executor = iced::executor::Default;
type Flags = ();

fn new() -> Self {
TabBarExample {
fn new(_flags: ()) -> (TabBarExample, Command<Message>) {
(
TabBarExample::Loading,
Command::batch(vec![
font::load(iced_aw::graphics::icons::ICON_FONT_BYTES).map(Message::FontLoaded),
Command::perform(load(), Message::Loaded),
]),
)
/*TabBarExample {
active_tab: 0,
new_tab_label: String::new(),
new_tab_content: String::new(),
tabs: Vec::new(),
}
}*/
}

fn title(&self) -> String {
String::from("TabBar example")
}

fn update(&mut self, message: Message) {
match message {
Message::TabSelected(index) => {
println!("Tab selected: {}", index);
self.active_tab = index
}
Message::TabClosed(index) => {
self.tabs.remove(index);
println!("active tab before: {}", self.active_tab);
self.active_tab = if self.tabs.is_empty() {
0
} else {
usize::max(0, usize::min(self.active_tab, self.tabs.len() - 1))
};
println!("active tab after: {}", self.active_tab);
}
Message::TabLabelInputChanged(value) => self.new_tab_label = value,
Message::TabContentInputChanged(value) => self.new_tab_content = value,
Message::NewTab => {
println!("New");
if !self.new_tab_label.is_empty() && !self.new_tab_content.is_empty() {
println!("Create");
self.tabs.push((
self.new_tab_label.to_owned(),
self.new_tab_content.to_owned(),
));
self.new_tab_label.clear();
self.new_tab_content.clear();
fn update(&mut self, message: Message) -> Command<Message> {
match self {
TabBarExample::Loading => {
if let Message::Loaded(_) = message {
*self = TabBarExample::Loaded(State {
active_tab: 0,
new_tab_label: String::new(),
new_tab_content: String::new(),
tabs: Vec::new(),
})
}
}
TabBarExample::Loaded(state) => match message {
Message::TabSelected(index) => {
println!("Tab selected: {}", index);
state.active_tab = index
}
Message::TabClosed(index) => {
state.tabs.remove(index);
println!("active tab before: {}", state.active_tab);
state.active_tab = if state.tabs.is_empty() {
0
} else {
usize::max(0, usize::min(state.active_tab, state.tabs.len() - 1))
};
println!("active tab after: {}", state.active_tab);
}
Message::TabLabelInputChanged(value) => state.new_tab_label = value,
Message::TabContentInputChanged(value) => state.new_tab_content = value,
Message::NewTab => {
println!("New");
if !state.new_tab_label.is_empty() && !state.new_tab_content.is_empty() {
println!("Create");
state.tabs.push((
state.new_tab_label.to_owned(),
state.new_tab_content.to_owned(),
));
state.new_tab_label.clear();
state.new_tab_content.clear();
}
}
_ => {}
},
}

Command::none()
}

fn view(&self) -> Element<Message> {
Column::new()
.push(
Row::new()
match self {
TabBarExample::Loading => container(
text("Loading...")
.horizontal_alignment(alignment::Horizontal::Center)
.size(50),
)
.width(Length::Fill)
.height(Length::Fill)
.center_y()
.center_x()
.into(),
TabBarExample::Loaded(state) => {
Column::new()
.push(
TextInput::new("Tab label", &self.new_tab_label)
.on_input(Message::TabLabelInputChanged)
.size(22)
.padding(5.0),
Row::new()
.push(
TextInput::new("Tab label", &state.new_tab_label)
.on_input(Message::TabLabelInputChanged)
.size(22)
.padding(5.0),
)
.push(
TextInput::new("Tab content", &state.new_tab_content)
.on_input(Message::TabContentInputChanged)
.size(22)
.padding(5.0),
)
.push(Button::new(Text::new("New")).on_press(Message::NewTab))
.align_items(Alignment::Center)
.padding(10.0)
.spacing(5.0),
)
.push(
TextInput::new("Tab content", &self.new_tab_content)
.on_input(Message::TabContentInputChanged)
.size(22)
.padding(5.0),
state
.tabs
.iter()
.fold(
TabBar::new(Message::TabSelected),
|tab_bar, (tab_label, _)| {
// manually create a new index for the new tab
// starting from 0, when there is no tab created yet
let idx = tab_bar.size();
tab_bar.push(idx, TabLabel::Text(tab_label.to_owned()))
},
)
.on_close(Message::TabClosed)
.tab_width(Length::Shrink)
.spacing(5.0)
.padding(5.0)
.text_size(32.0),
)
.push(Button::new(Text::new("New")).on_press(Message::NewTab))
.align_items(Alignment::Center)
.padding(10.0)
.spacing(5.0),
)
.push(
self.tabs
.iter()
.fold(
TabBar::new(Message::TabSelected),
|tab_bar, (tab_label, _)| {
// manually create a new index for the new tab
// starting from 0, when there is no tab created yet
let idx = tab_bar.size();
tab_bar.push(idx, TabLabel::Text(tab_label.to_owned()))
},
.push(
if let Some((_, content)) = state.tabs.get(state.active_tab) {
Text::new(content)
} else {
Text::new("Please create a new tab")
}
.size(25),
)
.on_close(Message::TabClosed)
.tab_width(Length::Shrink)
.spacing(5.0)
.padding(5.0)
.text_size(32.0),
)
.push(
if let Some((_, content)) = self.tabs.get(self.active_tab) {
Text::new(content)
} else {
Text::new("Please create a new tab")
}
.size(25),
)
.into()
.into()
}
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mod platform {

#[doc(no_inline)]
#[cfg(feature = "wrap")]
pub use {crate::native::wrap, wrap::Wrap};
pub use {crate::native::wrap, wrap::direction, wrap::Wrap};

#[doc(no_inline)]
#[cfg(feature = "number_input")]
Expand Down
Loading

0 comments on commit 80a3d30

Please sign in to comment.