Skip to content

Commit

Permalink
[awm2] Allow windows to update their titles
Browse files Browse the repository at this point in the history
  • Loading branch information
codyd51 committed Nov 27, 2022
1 parent 4dae953 commit 0a9eb8a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 22 deletions.
1 change: 1 addition & 0 deletions rust_programs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust_programs/awm2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ awm_messages = {path = "../awm_messages" }
libgui = { path = "../libgui", default-features = false }
mouse_driver_messages = {path = "../mouse_driver_messages" }
kb_driver_messages = {path = "../kb_driver_messages" }
file_manager_messages = {path = "../file_manager_messages" }
# PT: Just for holding a global RNG for random colors, not vital...
rand = { version = "0.8.5", default-features = false, features = ["small_rng"] }
lazy_static = { version = "1.4.0", default-features = false, features = ["spin_no_std"] }
Expand Down
50 changes: 38 additions & 12 deletions rust_programs/awm2/src/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ use alloc::rc::Rc;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use awm_messages::AwmCreateWindow;
use awm_messages::{AwmCreateWindow, AwmWindowUpdateTitle};
use axle_rt::core_commands::AmcSharedMemoryCreateRequest;
use core::cell::RefCell;
use core::cmp::{max, min};
use core::fmt::{Display, Formatter};
use mouse_driver_messages::MousePacket;

use file_manager_messages::str_from_u8_nul_utf8_unchecked;
use kb_driver_messages::{KeyEventType, KeyboardPacket};
use lazy_static::lazy_static;
use rand::rngs::SmallRng;
Expand Down Expand Up @@ -67,6 +68,7 @@ pub struct Window {
pub owner_service: String,
layer: RefCell<SingleFramebufferLayer>,
pub content_layer: RefCell<SingleFramebufferLayer>,
title: RefCell<Option<String>>,
}

impl Window {
Expand All @@ -80,14 +82,19 @@ impl Window {
owner_service: owner_service.to_string(),
layer: RefCell::new(SingleFramebufferLayer::new(total_size)),
content_layer: RefCell::new(window_layer),
title: RefCell::new(None),
}
}

fn set_frame(&self, frame: Rect) {
*self.frame.borrow_mut() = frame
}

fn redraw_title_bar(&self) {
fn set_title(&self, new_title: &str) {
*self.title.borrow_mut() = Some(new_title.to_string())
}

fn redraw_title_bar(&self) -> Rect {
let title_bar_frame = Rect::with_size(Size::new(
self.frame().width(),
Self::TITLE_BAR_HEIGHT as isize,
Expand All @@ -97,18 +104,22 @@ impl Window {

// Draw the window title
let font_size = Size::new(8, 12);
let window_title = &self.owner_service;
let maybe_window_title = self.title.borrow();
let window_title = maybe_window_title.as_ref().unwrap_or(&self.owner_service);
println!("Found title {window_title}");
let title_len = window_title.len();
let mut cursor = title_bar_frame.midpoint()
- Point::new(
(((font_size.width * (title_len as isize)) as f64) / 2.0) as isize,
(((font_size.height as f64) / 2.0) - 1.0) as isize,
);
let title_text_color = Color::new(50, 50, 50);
for ch in self.name().chars() {
for ch in window_title.chars() {
title_bar_slice.draw_char(ch, cursor, title_text_color, font_size);
cursor.x += font_size.width;
}

title_bar_frame.replace_origin(self.frame.borrow().origin)
}

pub fn render_remote_layer(&self) {
Expand Down Expand Up @@ -624,7 +635,6 @@ impl Desktop {
request: &AwmCreateWindow,
origin: Option<Point>,
) -> Rc<Window> {
// Make a copy of the source service early as it's shared amc memory
let source = source.to_string();
println!("Creating window of size {:?} for {}", request.size, source);

Expand Down Expand Up @@ -787,19 +797,35 @@ impl Desktop {
self.mouse_state.pos = pos
}

pub fn handle_window_requested_redraw(&mut self, window_owner: &str) {
// Find the window
let window = {
// PT: Assumes a single window per client
fn window_for_owner(&self, window_owner: &str) -> Rc<Window> {
// PT: Assumes a single window per client
Rc::clone(
self.windows
.iter()
.find(|w| w.owner_service == window_owner)
.expect(format!("Failed to find window for {}", window_owner).as_str())
};
.expect(format!("Failed to find window for {}", window_owner).as_str()),
)
}

pub fn handle_window_requested_redraw(&mut self, window_owner: &str) {
let window = self.window_for_owner(window_owner);
// Render the framebuffer to the visible window layer
window.render_remote_layer();
self.compositor_state
.queue_composite(Rc::clone(window) as Rc<dyn DesktopElement>)
.queue_composite(Rc::clone(&window) as Rc<dyn DesktopElement>)
}

pub fn handle_window_updated_title(&self, window_owner: &str, update: &AwmWindowUpdateTitle) {
let new_title = str_from_u8_nul_utf8_unchecked(&update.title);
println!("Window for {window_owner} updated title to {new_title}");
let window = self.window_for_owner(window_owner);
window.set_title(new_title);
let title_bar_frame = window.redraw_title_bar();
println!("Queueing extra draw {title_bar_frame}");
self.compositor_state.queue_extra_draw(
Rc::clone(&window) as Rc<dyn DesktopElement>,
title_bar_frame,
);
}

pub fn test(&mut self) {
Expand Down
18 changes: 15 additions & 3 deletions rust_programs/awm2/src/main_axle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use agx_definitions::{
Color, Drawable, Layer, LayerSlice, LikeLayerSlice, Line, NestedLayerSlice, Point, Rect,
RectInsets, SingleFramebufferLayer, Size, StrokeThickness,
};
use awm_messages::{AwmCreateWindow, AwmCreateWindowResponse, AwmWindowRedrawReady};
use awm_messages::{
AwmCreateWindow, AwmCreateWindowResponse, AwmWindowRedrawReady, AwmWindowUpdateTitle,
};

use kb_driver_messages::KB_DRIVER_SERVICE_NAME;
use mouse_driver_messages::{MousePacket, MOUSE_DRIVER_SERVICE_NAME};
Expand Down Expand Up @@ -118,8 +120,12 @@ pub fn main() {
//
// Wrap the whole thing in an unsafe block to reduce
// boilerplate in each match arm.
//
// Make a copy of the message source to pass around so that callers don't need to worry
// about its validity
let msg_source = msg_unparsed.source().to_string();
unsafe {
match msg_unparsed.source() {
match msg_source.as_str() {
MOUSE_DRIVER_SERVICE_NAME => match event {
MousePacket::EXPECTED_EVENT => {
desktop.handle_mouse_update(body_as_type_unchecked(raw_body))
Expand All @@ -141,7 +147,7 @@ pub fn main() {
// Keyboard events
AwmCreateWindow::EXPECTED_EVENT => {
desktop.spawn_window(
msg_unparsed.source(),
&msg_source,
body_as_type_unchecked(raw_body),
None,
);
Expand All @@ -150,6 +156,12 @@ pub fn main() {
//println!("Window said it was ready to redraw!");
desktop.handle_window_requested_redraw(msg_unparsed.source());
}
AwmWindowUpdateTitle::EXPECTED_EVENT => {
desktop.handle_window_updated_title(
&msg_source,
body_as_type_unchecked(raw_body),
);
}
_ => {
println!("Awm ignoring message with unknown event type: {event}");
}
Expand Down
10 changes: 8 additions & 2 deletions rust_programs/awm2/src/main_std.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::desktop::{Desktop, RenderStrategy};
use agx_definitions::{Color, Layer, LikeLayerSlice, Point, Rect, Size};
use alloc::rc::Rc;
use awm_messages::AwmCreateWindow;
use awm_messages::{AwmCreateWindow, AwmWindowUpdateTitle};
use libgui::PixelLayer;
use mouse_driver_messages::MousePacket;
use pixels::{Error, Pixels, SurfaceTexture};
Expand Down Expand Up @@ -146,7 +146,13 @@ pub fn main() -> Result<(), Box<dyn error::Error>> {
} => {
if let Some(key_code) = input.virtual_keycode {
match key_code {
VirtualKeyCode::A => w1.render_remote_layer(),
VirtualKeyCode::A => {
w1.render_remote_layer();
desktop.handle_window_updated_title(
"Window 0",
&AwmWindowUpdateTitle::new("New Title"),
);
}
VirtualKeyCode::B => w2.render_remote_layer(),
VirtualKeyCode::C => w3.render_remote_layer(),
VirtualKeyCode::D => desktop.test(),
Expand Down
7 changes: 2 additions & 5 deletions rust_programs/awm_messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,16 @@ impl ExpectsEventField for AwmWindowRedrawReady {

#[repr(C)]
#[derive(Debug, ContainsEventField)]
#[cfg(target_os = "axle")]
pub struct AwmWindowUpdateTitle {
event: u32,
title_len: u32,
title: [u8; 64],
pub title_len: u32,
pub title: [u8; 64],
}

#[cfg(target_os = "axle")]
impl ExpectsEventField for AwmWindowUpdateTitle {
const EXPECTED_EVENT: u32 = 813;
}

#[cfg(target_os = "axle")]
impl AwmWindowUpdateTitle {
pub fn new(title: &str) -> Self {
let mut title_buf = [0; 64];
Expand Down

0 comments on commit 0a9eb8a

Please sign in to comment.