Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify example feature requirements in a standard way. #1050

Merged
merged 1 commit into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ You can find its changes [documented below](#060---2020-06-01).

### Examples

- Specify feature requirements in a standard way. ([#1050] by [@xStrom])

### Maintenance

- Standardized web targeting terminology. ([#1013] by [@xStrom])
Expand Down Expand Up @@ -334,6 +336,7 @@ Last release without a changelog :(
[#1028]: https://github.com/xi-editor/druid/pull/1028
[#1042]: https://github.com/xi-editor/druid/pull/1042
[#1043]: https://github.com/xi-editor/druid/pull/1043
[#1050]: https://github.com/xi-editor/druid/pull/1050

[Unreleased]: https://github.com/xi-editor/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/xi-editor/druid/compare/v0.5.0...v0.6.0
Expand Down
12 changes: 12 additions & 0 deletions druid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ console_log = "0.2.0"
float-cmp = { version = "0.8.0", features = ["std"], default-features = false }
tempfile = "3.1.0"
piet-common = { version = "0.1.1", features = ["png"] }

[[example]]
name = "image"
required-features = ["image"]

[[example]]
name = "list"
required-features = ["im"]

[[example]]
name = "svg"
required-features = ["svg"]
71 changes: 30 additions & 41 deletions druid/examples/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,43 @@
// limitations under the License.

//! This example shows how to draw an png image.
//!
//! Requires the non-default "image" feature to be enabled:
//! `cargo run --example image --features "images"`
//!
#[cfg(not(feature = "image"))]
pub fn main() {
eprintln!("This examples requires the \"image\" feature to be enabled:");
eprintln!("cargo run --example image --features \"image\"");
}
use druid::{
widget::{FillStrat, Flex, Image, ImageData, WidgetExt},
AppLauncher, Color, Widget, WindowDesc,
};

#[cfg(feature = "image")]
pub fn main() {
use druid::{
widget::{FillStrat, Flex, Image, ImageData, WidgetExt},
AppLauncher, Color, Widget, WindowDesc,
};

fn ui_builder() -> impl Widget<u32> {
let png_data = ImageData::from_data(include_bytes!("./assets/PicWithAlpha.png")).unwrap();

let mut col = Flex::column();

col.add_flex_child(
Image::new(png_data.clone())
.border(Color::WHITE, 1.0)
.fix_width(100.0)
.center(),
1.0,
);

/*
// If you want to change the fill stratagy you can but you need the widget to be mut
let mut otherimage = Image::new(png_data);
otherimage.set_fill(FillStrat::FitWidth);
*/

let otherimage = Image::new(png_data)
.fill_mode(FillStrat::FitWidth)
.border(Color::WHITE, 1.0);
col.add_flex_child(otherimage, 1.0);
col
};

let main_window = WindowDesc::new(ui_builder);
let data = 0_u32;
AppLauncher::with_window(main_window)
.use_simple_logger()
.launch(data)
.expect("launch failed");
}

fn ui_builder() -> impl Widget<u32> {
let png_data = ImageData::from_data(include_bytes!("./assets/PicWithAlpha.png")).unwrap();

let mut col = Flex::column();

col.add_flex_child(
Image::new(png_data.clone())
.border(Color::WHITE, 1.0)
.fix_width(100.0)
.center(),
1.0,
);

/*
// If you want to change the fill stratagy you can but you need the widget to be mut
let mut otherimage = Image::new(png_data);
otherimage.set_fill(FillStrat::FitWidth);
*/

let otherimage = Image::new(png_data)
.fill_mode(FillStrat::FitWidth)
.border(Color::WHITE, 1.0);
col.add_flex_child(otherimage, 1.0);
col
}
196 changes: 91 additions & 105 deletions druid/examples/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,122 +14,108 @@

//! Demos basic list widget and list manipulations.
#[cfg(not(feature = "im"))]
pub fn main() {
eprintln!("This examples requires the \"im\" feature to be enabled:");
eprintln!("cargo run --example list --features im");
}
use druid::im::{vector, Vector};
use druid::lens::{self, LensExt};
use druid::widget::{Button, CrossAxisAlignment, Flex, Label, List, Scroll};
use druid::{
AppLauncher, Color, Data, Lens, LocalizedString, UnitPoint, Widget, WidgetExt, WindowDesc,
};

#[cfg(feature = "im")]
pub fn main() {
example::main()
#[derive(Clone, Data, Lens)]
struct AppData {
left: Vector<u32>,
right: Vector<u32>,
}

#[cfg(feature = "im")]
mod example {
use druid::im::{vector, Vector};
use druid::lens::{self, LensExt};
use druid::widget::{Button, CrossAxisAlignment, Flex, Label, List, Scroll};
use druid::{
AppLauncher, Color, Data, Lens, LocalizedString, UnitPoint, Widget, WidgetExt, WindowDesc,
pub fn main() {
let main_window = WindowDesc::new(ui_builder)
.title(LocalizedString::new("list-demo-window-title").with_placeholder("List Demo"));
// Set our initial data
let data = AppData {
left: vector![1, 2],
right: vector![1, 2, 3],
};
AppLauncher::with_window(main_window)
.use_simple_logger()
.launch(data)
.expect("launch failed");
}

#[derive(Clone, Data, Lens)]
struct AppData {
left: Vector<u32>,
right: Vector<u32>,
}

pub fn main() {
let main_window = WindowDesc::new(ui_builder)
.title(LocalizedString::new("list-demo-window-title").with_placeholder("List Demo"));
// Set our initial data
let data = AppData {
left: vector![1, 2],
right: vector![1, 2, 3],
};
AppLauncher::with_window(main_window)
.use_simple_logger()
.launch(data)
.expect("launch failed");
}

fn ui_builder() -> impl Widget<AppData> {
let mut root = Flex::column();
fn ui_builder() -> impl Widget<AppData> {
let mut root = Flex::column();

// Build a button to add children to both lists
root.add_child(
Button::new("Add")
.on_click(|_, data: &mut AppData, _| {
// Add child to left list
let value = data.left.len() + 1;
data.left.push_back(value as u32);
// Build a button to add children to both lists
root.add_child(
Button::new("Add")
.on_click(|_, data: &mut AppData, _| {
// Add child to left list
let value = data.left.len() + 1;
data.left.push_back(value as u32);

// Add child to right list
let value = data.right.len() + 1;
data.right.push_back(value as u32);
})
.fix_height(30.0)
.expand_width(),
);
// Add child to right list
let value = data.right.len() + 1;
data.right.push_back(value as u32);
})
.fix_height(30.0)
.expand_width(),
);

let mut lists = Flex::row().cross_axis_alignment(CrossAxisAlignment::Start);
let mut lists = Flex::row().cross_axis_alignment(CrossAxisAlignment::Start);

// Build a simple list
lists.add_flex_child(
Scroll::new(List::new(|| {
Label::new(|item: &u32, _env: &_| format!("List item #{}", item))
.align_vertical(UnitPoint::LEFT)
.padding(10.0)
.expand()
.height(50.0)
.background(Color::rgb(0.5, 0.5, 0.5))
}))
.vertical()
.lens(AppData::left),
1.0,
);
// Build a simple list
lists.add_flex_child(
Scroll::new(List::new(|| {
Label::new(|item: &u32, _env: &_| format!("List item #{}", item))
.align_vertical(UnitPoint::LEFT)
.padding(10.0)
.expand()
.height(50.0)
.background(Color::rgb(0.5, 0.5, 0.5))
}))
.vertical()
.lens(AppData::left),
1.0,
);

// Build a list with shared data
lists.add_flex_child(
Scroll::new(List::new(|| {
Flex::row()
.with_child(
Label::new(|(_, item): &(Vector<u32>, u32), _env: &_| {
format!("List item #{}", item)
// Build a list with shared data
lists.add_flex_child(
Scroll::new(List::new(|| {
Flex::row()
.with_child(
Label::new(|(_, item): &(Vector<u32>, u32), _env: &_| {
format!("List item #{}", item)
})
.align_vertical(UnitPoint::LEFT),
)
.with_flex_spacer(1.0)
.with_child(
Button::new("Delete")
.on_click(|_ctx, (shared, item): &mut (Vector<u32>, u32), _env| {
// We have access to both child's data and shared data.
// Remove element from right list.
shared.retain(|v| v != item);
})
.align_vertical(UnitPoint::LEFT),
)
.with_flex_spacer(1.0)
.with_child(
Button::new("Delete")
.on_click(|_ctx, (shared, item): &mut (Vector<u32>, u32), _env| {
// We have access to both child's data and shared data.
// Remove element from right list.
shared.retain(|v| v != item);
})
.fix_size(80.0, 20.0)
.align_vertical(UnitPoint::CENTER),
)
.padding(10.0)
.background(Color::rgb(0.5, 0.0, 0.5))
.fix_height(50.0)
}))
.vertical()
.lens(lens::Id.map(
// Expose shared data with children data
|d: &AppData| (d.right.clone(), d.right.clone()),
|d: &mut AppData, x: (Vector<u32>, Vector<u32>)| {
// If shared data was changed reflect the changes in our AppData
d.right = x.0
},
)),
1.0,
);
.fix_size(80.0, 20.0)
.align_vertical(UnitPoint::CENTER),
)
.padding(10.0)
.background(Color::rgb(0.5, 0.0, 0.5))
.fix_height(50.0)
}))
.vertical()
.lens(lens::Id.map(
// Expose shared data with children data
|d: &AppData| (d.right.clone(), d.right.clone()),
|d: &mut AppData, x: (Vector<u32>, Vector<u32>)| {
// If shared data was changed reflect the changes in our AppData
d.right = x.0
},
)),
1.0,
);

root.add_flex_child(lists, 1.0);
root.add_flex_child(lists, 1.0);

// Mark the widget as needing its layout rects painted
root.debug_paint_layout()
}
// Mark the widget as needing its layout rects painted
root.debug_paint_layout()
}
Loading