Skip to content

Commit 4feacc0

Browse files
author
Billy Messenger
authored
Merge pull request #41 from ilya-epifanov/fonts_and_optional_window_messages
Fonts and optional window messages
2 parents b7c47cf + c4eb92e commit 4feacc0

File tree

7 files changed

+29
-10
lines changed

7 files changed

+29
-10
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ categories = ["gui"]
1919
debug = ["iced_runtime/debug"]
2020
# Enable the wgu renderer
2121
wgpu = ["iced_renderer/wgpu"]
22+
image = ["iced_widget/image"]
23+
svg = ["iced_widget/svg"]
24+
canvas = ["iced_widget/canvas"]
2225

2326
[dependencies]
2427
baseview = { git = "https://github.com/RustAudio/baseview.git", rev = "fdb43ea" }

examples/custom_styles.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn main() {
2424
always_redraw: true,
2525
},
2626
flags: (),
27+
fonts: Default::default(),
2728
};
2829

2930
open_blocking::<MyProgram>(settings);

examples/hello_world.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn main() {
2323
always_redraw: true,
2424
},
2525
flags: (),
26+
fonts: Default::default(),
2627
};
2728

2829
open_blocking::<MyProgram>(settings);

examples/window_subs.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use iced_baseview::{
1212
Application, Settings,
1313
};
1414
use iced_runtime::window::Action;
15-
use std::time::{Duration, Instant};
15+
use std::{sync::Arc, time::{Duration, Instant}};
1616

1717
static COUNT_INTERVAL: Duration = Duration::from_millis(1000);
1818

@@ -28,6 +28,7 @@ fn main() {
2828
always_redraw: true,
2929
},
3030
flags: (),
31+
fonts: Default::default(),
3132
};
3233

3334
open_blocking::<MyProgram>(settings);
@@ -62,8 +63,8 @@ impl Application for MyProgram {
6263
}
6364

6465
fn subscription(&self, window_subs: &mut WindowSubs<Message>) -> Subscription<Message> {
65-
window_subs.on_frame = Some(|| Message::OnFrame);
66-
window_subs.on_window_will_close = Some(|| Message::WillClose);
66+
window_subs.on_frame = Some(Arc::new(|| Some(Message::OnFrame)));
67+
window_subs.on_window_will_close = Some(Arc::new(|| Some(Message::WillClose)));
6768
Subscription::none()
6869
}
6970

src/application.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,19 @@ where
167167
};
168168

169169
let compositor_settings = A::renderer_settings();
170-
let (mut compositor, renderer) = C::new(compositor_settings, Some(window))?;
170+
let (mut compositor, mut renderer) = C::new(compositor_settings, Some(window))?;
171171
let surface = compositor.create_surface(
172172
window,
173173
viewport.physical_width(),
174174
viewport.physical_height(),
175175
);
176176

177+
for font in settings.fonts {
178+
use crate::core::text::Renderer;
179+
180+
renderer.load_font(font);
181+
}
182+
177183
let (window_queue, window_queue_rx) = WindowQueue::new();
178184
let event_status = Rc::new(RefCell::new(baseview::EventStatus::Ignored));
179185

@@ -281,7 +287,9 @@ async fn run_instance<A, E, C>(
281287
match event {
282288
RuntimeEvent::MainEventsCleared => {
283289
if let Some(message) = &window_subs.on_frame {
284-
messages.push(message());
290+
if let Some(message) = message() {
291+
messages.push(message);
292+
}
285293
}
286294

287295
if !did_process_event
@@ -518,7 +526,9 @@ async fn run_instance<A, E, C>(
518526
if let Some(message) = &window_subs.on_window_will_close {
519527
// Send message to user before exiting the loop.
520528

521-
messages.push(message());
529+
if let Some(message) = message() {
530+
messages.push(message);
531+
}
522532
let mut cache = ManuallyDrop::into_inner(user_interface).into_cache();
523533

524534
update(

src/settings.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Configure your application.
2-
use std::fmt::Debug;
2+
use std::{borrow::Cow, fmt::Debug};
33

44
use baseview::WindowOpenOptions;
55

@@ -20,6 +20,9 @@ pub struct Settings<Flags> {
2020

2121
/// iced_baseview settings
2222
pub iced_baseview: IcedBaseviewSettings,
23+
24+
/// The fonts to load on boot.
25+
pub fonts: Vec<Cow<'static, [u8]>>,
2326
}
2427

2528
/// Any settings specific to `iced_baseview`.

src/window.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, pin::Pin, rc::Rc};
1+
use std::{cell::RefCell, pin::Pin, rc::Rc, sync::Arc};
22

33
use baseview::{Event, EventStatus, Window, WindowHandler, WindowOpenOptions};
44
use iced_runtime::futures::futures::{
@@ -284,9 +284,9 @@ impl WindowQueue {
284284
#[allow(missing_debug_implementations)]
285285
pub struct WindowSubs<Message> {
286286
/// The message to send right before each rendering frame.
287-
pub on_frame: Option<fn() -> Message>,
287+
pub on_frame: Option<Arc<dyn Fn() -> Option<Message>>>,
288288
/// The message to send when the window is about to close.
289-
pub on_window_will_close: Option<fn() -> Message>,
289+
pub on_window_will_close: Option<Arc<dyn Fn() -> Option<Message>>>,
290290
}
291291

292292
impl<Message> Default for WindowSubs<Message> {

0 commit comments

Comments
 (0)