examples: randomise window location in simple.rs

This commit is contained in:
Victor Berger 2017-09-05 21:23:40 +02:00
parent db6bad1676
commit 30c0628959
2 changed files with 31 additions and 10 deletions

View File

@ -26,6 +26,7 @@ gl_generator = "0.5"
[dev-dependencies] [dev-dependencies]
slog-term = "2.0" slog-term = "2.0"
slog-async = "2.0" slog-async = "2.0"
rand = "0.3"
[features] [features]
default = ["backend_winit", "backend_libinput", "renderer_glium"] default = ["backend_winit", "backend_libinput", "renderer_glium"]

View File

@ -1,16 +1,16 @@
#[macro_use] #[macro_use]
extern crate glium; extern crate glium;
extern crate rand;
#[macro_use]
extern crate slog;
extern crate slog_async;
extern crate slog_term;
#[macro_use(define_roles)] #[macro_use(define_roles)]
extern crate smithay; extern crate smithay;
extern crate wayland_protocols; extern crate wayland_protocols;
#[macro_use(server_declare_handler)] #[macro_use(server_declare_handler)]
extern crate wayland_server; extern crate wayland_server;
#[macro_use]
extern crate slog;
extern crate slog_async;
extern crate slog_term;
mod helpers; mod helpers;
use glium::Surface; use glium::Surface;
@ -42,7 +42,7 @@ struct SurfaceHandler {
#[derive(Default)] #[derive(Default)]
struct SurfaceData { struct SurfaceData {
buffer: Option<(Vec<u8>, (u32, u32))>, buffer: Option<(Vec<u8>, (u32, u32))>,
location: Option<(u32, u32)>, location: Option<(i32, i32)>,
} }
impl compositor::Handler<SurfaceData, Roles> for SurfaceHandler { impl compositor::Handler<SurfaceData, Roles> for SurfaceHandler {
@ -77,7 +77,15 @@ impl compositor::Handler<SurfaceData, Roles> for SurfaceHandler {
} }
} }
struct ShellSurfaceHandler; struct ShellSurfaceHandler {
token: CompositorToken<SurfaceData, Roles, SurfaceHandler>,
}
impl ShellSurfaceHandler {
fn new(token: CompositorToken<SurfaceData, Roles, SurfaceHandler>) -> ShellSurfaceHandler {
ShellSurfaceHandler { token }
}
}
impl shell::Handler<SurfaceData, Roles, SurfaceHandler, ()> for ShellSurfaceHandler { impl shell::Handler<SurfaceData, Roles, SurfaceHandler, ()> for ShellSurfaceHandler {
fn new_client(&mut self, evlh: &mut EventLoopHandle, client: ShellClient<()>) {} fn new_client(&mut self, evlh: &mut EventLoopHandle, client: ShellClient<()>) {}
@ -85,6 +93,16 @@ impl shell::Handler<SurfaceData, Roles, SurfaceHandler, ()> for ShellSurfaceHand
fn new_toplevel(&mut self, evlh: &mut EventLoopHandle, fn new_toplevel(&mut self, evlh: &mut EventLoopHandle,
surface: ToplevelSurface<SurfaceData, Roles, SurfaceHandler, ()>) surface: ToplevelSurface<SurfaceData, Roles, SurfaceHandler, ()>)
-> ToplevelConfigure { -> ToplevelConfigure {
let wl_surface = surface.get_surface().unwrap();
self.token.with_surface_data(wl_surface, |data| {
// place the window at a random location in the [0;300]x[0;300] square
use rand::distributions::{IndependentSample, Range};
let range = Range::new(0, 300);
let mut rng = rand::thread_rng();
let x = range.ind_sample(&mut rng);
let y = range.ind_sample(&mut rng);
data.user_data.location = Some((x, y))
});
ToplevelConfigure { ToplevelConfigure {
size: None, size: None,
states: vec![], states: vec![],
@ -185,8 +203,8 @@ fn main() {
* Initialize the shell global * Initialize the shell global
*/ */
let shell_handler_id = event_loop.add_handler_with_init(MyShellHandler::new( let shell_handler_id = event_loop.add_handler_with_init(MyShellHandler::new(
ShellSurfaceHandler, ShellSurfaceHandler::new(compositor_token),
compositor_token.clone(), compositor_token,
log.clone(), log.clone(),
)); ));
event_loop.register_global::<wl_shell::WlShell, MyShellHandler>(shell_handler_id, 1); event_loop.register_global::<wl_shell::WlShell, MyShellHandler>(shell_handler_id, 1);
@ -220,9 +238,11 @@ fn main() {
{ {
if let Some(wl_surface) = toplevel_surface.get_surface() { if let Some(wl_surface) = toplevel_surface.get_surface() {
// this surface is a root of a subsurface tree that needs to be drawn // this surface is a root of a subsurface tree that needs to be drawn
let initial_place = compositor_token
.with_surface_data(wl_surface, |data| data.user_data.location.unwrap_or((0, 0)));
compositor_token.with_surface_tree( compositor_token.with_surface_tree(
wl_surface, wl_surface,
(100, 100), initial_place,
|surface, attributes, role, &(mut x, mut y)| { |surface, attributes, role, &(mut x, mut y)| {
if let Some((ref contents, (w, h))) = attributes.user_data.buffer { if let Some((ref contents, (w, h))) = attributes.user_data.buffer {
// there is actually something to draw ! // there is actually something to draw !