Properly paint for restarted client, do not recreate painters on crash
This commit is contained in:
@@ -9,7 +9,7 @@ use rect::Rect;
|
||||
|
||||
/// A painter that paints on a pixelflut panel.
|
||||
pub struct Painter {
|
||||
client: Client,
|
||||
client: Option<Client>,
|
||||
area: Rect,
|
||||
offset: (u32, u32),
|
||||
image: Option<DynamicImage>,
|
||||
@@ -18,7 +18,7 @@ pub struct Painter {
|
||||
impl Painter {
|
||||
/// Create a new painter.
|
||||
pub fn new(
|
||||
client: Client,
|
||||
client: Option<Client>,
|
||||
area: Rect,
|
||||
offset: (u32, u32),
|
||||
image: Option<DynamicImage>,
|
||||
@@ -71,13 +71,15 @@ impl Painter {
|
||||
let color = Color::from(channels[0], channels[1], channels[2]);
|
||||
|
||||
// Set the pixel
|
||||
self.client.write_pixel(
|
||||
if let Some(client) = &mut self.client {
|
||||
client.write_pixel(
|
||||
x + self.area.x + self.offset.0,
|
||||
y + self.area.y + self.offset.1,
|
||||
color,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Everything seems to be ok
|
||||
Ok(())
|
||||
@@ -87,4 +89,9 @@ impl Painter {
|
||||
pub fn set_image(&mut self, image: DynamicImage) {
|
||||
self.image = Some(image);
|
||||
}
|
||||
|
||||
/// Update the client.
|
||||
pub fn set_client(&mut self, client: Option<Client>) {
|
||||
self.client = client;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,9 +69,13 @@ impl Canvas {
|
||||
|
||||
// Create the painter thread
|
||||
let thread = thread::spawn(move || {
|
||||
// Create the painter
|
||||
let mut painter = Painter::new(None, area, offset, None);
|
||||
|
||||
loop {
|
||||
// The painting loop
|
||||
'paint: loop {
|
||||
// Create a new client
|
||||
// Connect
|
||||
let client = match Client::connect(host.clone()) {
|
||||
Ok(client) => client,
|
||||
Err(e) => {
|
||||
@@ -79,9 +83,7 @@ impl Canvas {
|
||||
break 'paint;
|
||||
},
|
||||
};
|
||||
|
||||
// Create a painter
|
||||
let mut painter = Painter::new(client, area, offset, None);
|
||||
painter.set_client(Some(client));
|
||||
|
||||
// Keep painting
|
||||
loop {
|
||||
|
||||
Reference in New Issue
Block a user