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

Modify child_window example #4054

Merged
merged 21 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
22 changes: 18 additions & 4 deletions examples/child_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ fn main() -> Result<(), impl std::error::Error> {
event: KeyEvent { state: ElementState::Pressed, .. },
..
} => {
let child_index = self.windows.len() - 1;
let parent_window = self.windows.get(&self.parent_window_id.unwrap()).unwrap();
let child_window = spawn_child_window(parent_window.as_ref(), event_loop);
let child_window =
spawn_child_window(parent_window.as_ref(), event_loop, child_index as i32);
let child_id = child_window.id();
println!("Child window created with id: {child_id:?}");
self.windows.insert(child_id, child_window);
},
WindowEvent::RedrawRequested => {
if let Some(window) = self.windows.get(&window_id) {
fill::fill_window(window.as_ref());
if window_id == self.parent_window_id.unwrap() {
fill::fill_window(window.as_ref(), 0xff181818);
} else {
fill::fill_window(window.as_ref(), 0xffbbbbbb);
Sl-L marked this conversation as resolved.
Show resolved Hide resolved
}
}
},
_ => (),
Expand All @@ -75,12 +81,20 @@ fn main() -> Result<(), impl std::error::Error> {
fn spawn_child_window(
parent: &dyn Window,
event_loop: &dyn ActiveEventLoop,
child_count: i32,
) -> Box<dyn Window> {
let parent = parent.raw_window_handle().unwrap();

// As child count increases, x goes from 0*128 to 5*128 and then repeats
let x: f64 = child_count.rem_euclid(5) as f64 * 128.0;

// After 5 windows have been put side by side horizontally, a new row starts
let y: f64 = (child_count / 5) as f64 * 96.0;

let mut window_attributes = WindowAttributes::default()
.with_title("child window")
.with_surface_size(LogicalSize::new(200.0f32, 200.0f32))
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
.with_surface_size(LogicalSize::new(128.0f32, 96.0))
.with_position(Position::Logical(LogicalPosition::new(x, y)))
.with_visible(true);
// `with_parent_window` is unsafe. Parent window must be a valid window.
window_attributes = unsafe { window_attributes.with_parent_window(Some(parent)) };
Expand Down
2 changes: 1 addition & 1 deletion examples/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl ApplicationHandler for ControlFlowDemo {
WindowEvent::RedrawRequested => {
let window = self.window.as_ref().unwrap();
window.pre_present_notify();
fill::fill_window(window.as_ref());
fill::fill_window(window.as_ref(), 0xff181818);
},
_ => (),
}
Expand Down
2 changes: 1 addition & 1 deletion examples/pump_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() -> std::process::ExitCode {
match event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
fill::fill_window(window.as_ref());
fill::fill_window(window.as_ref(), 0xff181818);
window.request_redraw();
},
_ => (),
Expand Down
2 changes: 1 addition & 1 deletion examples/run_on_demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
self.window = None;
},
WindowEvent::RedrawRequested => {
fill::fill_window(window.as_ref());
fill::fill_window(window.as_ref(), 0xff181818);
},
_ => (),
}
Expand Down
9 changes: 4 additions & 5 deletions examples/util/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod platform {
}
}

pub fn fill_window(window: &dyn Window) {
pub fn fill_window(window: &dyn Window, color: u32) {
Sl-L marked this conversation as resolved.
Show resolved Hide resolved
GC.with(|gc| {
let size = window.surface_size();
let (Some(width), Some(height)) =
Expand All @@ -84,13 +84,12 @@ mod platform {
let surface =
gc.get_or_insert_with(|| GraphicsContext::new(window)).create_surface(window);

// Fill a buffer with a solid color.
const DARK_GRAY: u32 = 0xff181818;
// Fill a buffer with a solid color

surface.resize(width, height).expect("Failed to resize the softbuffer surface");

let mut buffer = surface.buffer_mut().expect("Failed to get the softbuffer buffer");
buffer.fill(DARK_GRAY);
buffer.fill(color);
buffer.present().expect("Failed to present the softbuffer buffer");
})
}
Expand All @@ -108,7 +107,7 @@ mod platform {

#[cfg(any(target_os = "android", target_os = "ios"))]
mod platform {
pub fn fill_window(_window: &dyn winit::window::Window) {
pub fn fill_window(_window: &dyn winit::window::Window, _color: u32) {
// No-op on mobile platforms.
}

Expand Down
2 changes: 1 addition & 1 deletion examples/x11_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main() -> Result<(), Box<dyn Error>> {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
window.pre_present_notify();
fill::fill_window(window.as_ref());
fill::fill_window(window.as_ref(), 0xff181818);
},
_ => (),
}
Expand Down
Loading