Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion twm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ log = "0.4"
syn = "1.0.38"
flexi_logger = "0.15"
reqwest = { version = "0.10", features = ["blocking", "json"] }
winapi = { version = "0.3", features = ["winuser", "errhandlingapi", "impl-default", "shellapi", "windowsx", "shellscalingapi", "processthreadsapi", "psapi"] }
winapi = { version = "0.3", features = ["winuser", "errhandlingapi", "impl-default", "shellapi", "windowsx", "shellscalingapi", "processthreadsapi", "psapi", "dwmapi"] }
serde = "1.0"
serde_json = "1.0"
chrono = "0.4"
Expand Down
12 changes: 4 additions & 8 deletions twm/src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use crate::{system::DisplayId, window::Window, AppState};
use item::Item;
use crate::{SystemResult, SystemError};
use crate::{SystemResult, SystemError, util};
use item_section::ItemSection;
use parking_lot::Mutex;

Expand Down Expand Up @@ -49,14 +49,10 @@ impl Bar {

pub fn change_height(&self, height: i32) -> SystemResult {
let nwin = self.window.get_native_window();
let display = nwin.get_display().unwrap();
let mut rect = nwin.get_rect()?;

if rect.top == 0 {
rect.bottom = height;
} else {
rect.top = rect.bottom - height;
}

let height = util::points_to_pixels(height, &display);
rect.bottom = rect.top + height;
nwin.set_window_pos(rect, None, None).map_err(|e| SystemError::Unknown(e))
}
}
Expand Down
52 changes: 25 additions & 27 deletions twm/src/bar/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
};
use crate::{
config::Config, display::Display, event::Event, system::DisplayId, system::Rectangle,
window::Api, window::WindowEvent, AppState, NOG_BAR_NAME,
window::Api, window::WindowEvent, AppState, NOG_BAR_NAME, util,
};
use log::{debug, error, info};
use mlua::Result as RuntimeResult;
Expand Down Expand Up @@ -47,11 +47,12 @@ fn draw_component_text(
fn draw_components(
api: &Api,
config: &Config,
display_id: DisplayId,
display: &Display,
mut offset: i32,
components: &[Component],
) -> RuntimeResult<()> {
for component in components {
let display_id = display.id;
let component_texts = component.render(display_id)?;

for (_i, component_text) in component_texts.iter().enumerate() {
Expand All @@ -62,7 +63,7 @@ fn draw_components(
let rect = Rectangle {
left: offset,
right: offset + width,
bottom: config.bar.height,
bottom: util::points_to_pixels(config.bar.height, &display),
top: 0,
};

Expand Down Expand Up @@ -115,11 +116,12 @@ fn components_to_section(
Ok(section)
}

fn clear_section(api: &Api, config: &Config, left: i32, right: i32) {
api.fill_rect(left, 0, right - left, config.bar.height, config.bar.color)
fn clear_section(api: &Api, config: &Config, left: i32, right: i32, display: &Display) {
let height = util::points_to_pixels(config.bar.height, display);
api.fill_rect(left, 0, right - left, height, config.bar.color)
}

pub fn create(state_arc: Arc<Mutex<AppState>>) {
pub fn create_or_update(state_arc: Arc<Mutex<AppState>>) {
info!("Creating appbar");

let sender = state_arc
Expand All @@ -141,11 +143,9 @@ pub fn create(state_arc: Arc<Mutex<AppState>>) {
.config
.clone();

if display.appbar.is_some() {
error!(
"Appbar for monitor {:?} already exists. Aborting",
display.id
);
if let Some(existing_bar) = display.appbar {
// Change height of existing bars in case the display has changed
existing_bar.change_height(config.bar.height);
continue;
}

Expand All @@ -154,8 +154,9 @@ pub fn create(state_arc: Arc<Mutex<AppState>>) {

bar.display_id = display.id;

let height = util::points_to_pixels(config.bar.height, &display);
let left = display.working_area_left();
let top = display.working_area_top(&config) - config.bar.height;
let top = display.working_area_top(&config) - height;
let width = display.working_area_width(&config);

bar.window = bar
Expand All @@ -168,28 +169,25 @@ pub fn create(state_arc: Arc<Mutex<AppState>>) {
.with_font_size(config.bar.font_size)
.with_background_color(config.bar.color)
.with_pos(left, top)
.with_size(width, config.bar.height);
.with_size(width, height);

let sender = sender.clone();
let state_arc2 = state_arc.clone();

bar.window.create(state_arc.clone(), true, move |event| {
match event {
WindowEvent::Native {
WindowEvent::AppBar {
msg, display_id, ..
} => {
//TODO: make this cleaner
#[cfg(target_os = "windows")]
{
use winapi::um::shellapi::ABN_FULLSCREENAPP;
use winapi::um::winuser::WM_APP;

if msg.code == WM_APP + 1 {
if msg.params.0 == ABN_FULLSCREENAPP as usize {
sender
.send(Event::ToggleAppbar(*display_id))
.expect("Failed to send ToggleAppbar event");
}
if msg.params.0 == ABN_FULLSCREENAPP as usize {
sender
.send(Event::ToggleAppbar(*display_id))
.expect("Failed to send ToggleAppbar event");
}
}
}
Expand Down Expand Up @@ -283,35 +281,35 @@ pub fn create(state_arc: Arc<Mutex<AppState>>) {
draw_components(
api,
&config,
*display_id,
&display,
left.left,
&config.bar.components.left,
)?;
draw_components(
api,
&config,
*display_id,
&display,
center.left,
&config.bar.components.center,
)?;
draw_components(
api,
&config,
*display_id,
&display,
right.left,
&config.bar.components.right,
)?;

if bar.left.width() > left.width() {
clear_section(api, &config, left.right, bar.left.right);
clear_section(api, &config, left.right, bar.left.right, &display);
}

if bar.center.width() > center.width() {
clear_section(api, &config, bar.center.left, bar.center.right);
clear_section(api, &config, bar.center.left, bar.center.right, &display);
}

if bar.right.width() > right.width() {
clear_section(api, &config, bar.right.left, right.left);
clear_section(api, &config, bar.right.left, right.left, &display);
}

sender
Expand Down
121 changes: 79 additions & 42 deletions twm/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use crate::{
task_bar,
tile_grid::store::Store,
tile_grid::TileGrid,
util,
};
use std::cmp::Ordering;
use task_bar::{Taskbar, TaskbarPosition};
use log::info;

#[derive(Default, Debug, Clone)]
pub struct Display {
Expand Down Expand Up @@ -57,19 +59,22 @@ impl Display {
let tb_height = self
.taskbar
.clone()
.map(|tb| match tb.get_position() {
// Should probably handle the error at some point instead of just unwraping
TaskbarPosition::Top | TaskbarPosition::Bottom => {
tb.window.get_rect().unwrap().height()
}
_ => 0,
.and_then(|tb| {
tb.get_position()
.and_then(|tbp| match tbp {
TaskbarPosition::Top | TaskbarPosition::Bottom => {
tb.window.get_rect().map(|tbr| tbr.height())
}
_ => Ok(0),
})
.ok()
})
.unwrap_or(0);

self.height()
- if config.remove_task_bar { 0 } else { tb_height }
- if config.display_app_bar {
config.bar.height
util::points_to_pixels(config.bar.height, &self)
} else {
0
}
Expand All @@ -78,12 +83,15 @@ impl Display {
let tb_width = self
.taskbar
.clone()
.map(|tb| match tb.get_position() {
// Should probably handle the error at some point instead of just unwraping
TaskbarPosition::Left | TaskbarPosition::Right => {
tb.window.get_rect().unwrap().width()
}
_ => 0,
.and_then(|tb| {
tb.get_position()
.and_then(|tbp| match tbp {
TaskbarPosition::Left | TaskbarPosition::Right => {
tb.window.get_rect().map(|tbr| tbr.width())
}
_ => Ok(0),
})
.ok()
})
.unwrap_or(0);

Expand All @@ -93,16 +101,19 @@ impl Display {
let offset = self
.taskbar
.clone()
.map(|t| match t.get_position() {
// Should probably handle the error at some point instead of just unwraping
TaskbarPosition::Top => t.window.get_rect().unwrap().height(),
_ => 0,
.and_then(|tb| {
tb.get_position()
.and_then(|tbp| match tbp {
TaskbarPosition::Top => tb.window.get_rect().map(|tbr| tbr.height()),
_ => Ok(0),
})
.ok()
})
.unwrap_or(0);

self.rect.top
+ if config.display_app_bar {
config.bar.height
util::points_to_pixels(config.bar.height, &self)
} else {
0
}
Expand All @@ -112,10 +123,13 @@ impl Display {
let offset = self
.taskbar
.clone()
.map(|t| match t.get_position() {
// Should probably handle the error at some point instead of just unwraping
TaskbarPosition::Left => t.window.get_rect().unwrap().width(),
_ => 0,
.and_then(|tb| {
tb.get_position()
.and_then(|tbp| match tbp {
TaskbarPosition::Left => tb.window.get_rect().map(|tbr| tbr.width()),
_ => Ok(0),
})
.ok()
})
.unwrap_or(0);

Expand Down Expand Up @@ -196,15 +210,18 @@ impl Display {
}
}

pub fn init(config: &Config) -> Vec<Display> {
pub fn init(config: &Config, old_displays: Option<&Vec<Display>>) -> Vec<Display> {
let mut displays = api::get_displays();

let taskbars = api::get_taskbars();

for d in displays.iter_mut() {
for tb in &taskbars {
let display = tb.window.get_display().unwrap();
if display.id == d.id {
d.taskbar = Some(tb.clone());
let display = tb.window.get_display();
if let Ok(display) = display {
if display.id == d.id {
d.taskbar = Some(tb.clone());
}
}
}
}
Expand All @@ -227,28 +244,48 @@ pub fn init(config: &Config) -> Vec<Display> {
ordering
});

for i in 1..11 {
let monitor = config
.workspaces
.iter()
.find(|s| s.id == i)
.map(|s| s.monitor)
.unwrap_or(-1);
if let Some(old_displays) = old_displays {
for old_display in old_displays.iter() {
let new_display = displays.iter_mut().find(|d| d.id == old_display.id);
if let Some(mut new_display) = new_display {
new_display.grids = old_display.grids.clone();
new_display.focused_grid_id = old_display.focused_grid_id;
new_display.appbar = old_display.appbar.clone();
} else {
let primary = displays.iter_mut().find(|d| d.is_primary());
let primary = match primary {
Some(p) => p,
None => &mut displays[0]
};
if let Some(appbar) = &old_display.appbar {
appbar.window.close();
}
primary.grids.append(&mut old_display.grids.clone());
}
}
} else {
for i in 1..11 {
let monitor = config
.workspaces
.iter()
.find(|s| s.id == i)
.map(|s| s.monitor)
.unwrap_or(-1);

let grid = TileGrid::new(i, renderer::NativeRenderer);
let grid = TileGrid::new(i, renderer::NativeRenderer);

if let Some(d) = displays.get_mut((monitor - 1) as usize) {
d.grids.push(grid);
} else {
for d in displays.iter_mut() {
if d.is_primary() {
d.grids.push(grid);
break;
if let Some(d) = displays.get_mut((monitor - 1) as usize) {
d.grids.push(grid);
} else {
for d in displays.iter_mut() {
if d.is_primary() {
d.grids.push(grid);
break;
}
}
}
}
}

displays

// task_bar::update_task_bars();
Expand Down
2 changes: 1 addition & 1 deletion twm/src/hot_reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn update_config(state_arc: Arc<Mutex<AppState>>, new_config: Config) -> Sys

if draw_app_bar {
drop(state);
bar::create::create(state_arc.clone());
bar::create::create_or_update(state_arc.clone());
state = state_arc.lock();
}

Expand Down
Loading