Skip to content

Commit

Permalink
implement new method of starting winbar, add show and hide subcommands (
Browse files Browse the repository at this point in the history
#1)

This commit fixes winbar not continuing to run after being started with winbarc and closing the terminal. An introduction of a new way to start winbar was implemented to remedy this. As a result of this, an issue arose in development where the status bar wouldn't properly be shown if winbarc was run using cargo run in debug mode. For now, release mode will need to be specified if choosing to run winbarc in this way.

A fix was also made to the status bar being invisible when winbar was started in some cases. See #1 for details.

Two new winbarc subcommands were also implemented for hiding and showing the status bar.
  • Loading branch information
Sulaxan authored May 5, 2024
1 parent 687ebf1 commit 6f023f2
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 21 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ That's the gist of it! There are more commands and options available that you ca
free to use the `--help` option on any command (`winbar`, `winbarc`) or subcommand to see everything
available to you.

## Compiling from source

Clone this repo and change into the project's directory. You can then compile using the following
command:

```
cargo build [--release]
```

If you want to install `winbar` and `winbarc`, run the following commands with the respective
project:

```
cargo install --path <./winbar | ./winbarc>
```

### A note on developing

For an unknown reason, `winbarc` does not properly start `winbar` if `winbarc` is run using `cargo
run`. If you wish to use `winbarc` to start `winbar`, make sure to install it using `cargo install`.

## Roadmap

- More configuration options for existing components
Expand Down
14 changes: 10 additions & 4 deletions winbar/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use windows::{
UI::WindowsAndMessaging::{
CreateWindowExW, DefWindowProcW, DispatchMessageW, PeekMessageW, PostQuitMessage,
RegisterClassW, SetLayeredWindowAttributes, ShowWindow, TranslateMessage, LWA_COLORKEY,
MSG, PM_REMOVE, SW_SHOWDEFAULT, WM_CLOSE, WM_DESTROY, WM_PAINT, WNDCLASSW,
MSG, PM_REMOVE, SW_SHOWNORMAL, WM_CLOSE, WM_DESTROY, WM_PAINT, WNDCLASSW,
WS_EX_LAYERED, WS_EX_TOOLWINDOW, WS_POPUP, WS_VISIBLE,
},
},
Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn create_window() -> HWND {

SetLayeredWindowAttributes(hwnd, COLORREF(TRANSPARENT_COLOR), 25, LWA_COLORKEY).ok();

let _success = ShowWindow(hwnd, SW_SHOWDEFAULT);
let _success = ShowWindow(hwnd, SW_SHOWNORMAL);

hwnd
}
Expand All @@ -72,12 +72,18 @@ pub fn listen(hwnd: HWND, recv: Receiver<WinbarAction>) {
loop {
if let Ok(action) = recv.try_recv() {
match action {
WinbarAction::Shutdown => {
WindowsApi::send_window_shutdown_msg(hwnd);
}
WinbarAction::UpdateWindow => unsafe {
// UpdateWindow(hwnd);
InvalidateRect(hwnd, None, true);
},
WinbarAction::Shutdown => {
WindowsApi::send_window_shutdown_msg(hwnd);
WinbarAction::ShowWindow => {
WindowsApi::show_window(hwnd);
}
WinbarAction::HideWindow => {
WindowsApi::hide_window(hwnd);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion winbar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ pub const DEFAULT_PORT: i32 = 10989;
pub const DEFAULT_HOSTNAME: &str = "localhost";

pub enum WinbarAction {
UpdateWindow,
Shutdown,
UpdateWindow,
ShowWindow,
HideWindow,
}

#[derive(Getters, Clone)]
Expand Down
4 changes: 3 additions & 1 deletion winbar/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use serde::{Deserialize, Serialize};
/// A message sent to the server by the client.
#[derive(Debug, Serialize, Deserialize)]
pub enum ServerMessage {
UpdateWindow,
Shutdown,
UpdateWindow,
ShowWindow,
HideWindow,
}

/// A server-bound payload.
Expand Down
14 changes: 10 additions & 4 deletions winbar/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::{
task::JoinHandle,
};
use winbar::{
protocol::{WinbarClientPayload, WinbarServerPayload},
protocol::{ServerMessage, WinbarClientPayload, WinbarServerPayload},
WinbarAction, WinbarContext,
};

Expand Down Expand Up @@ -93,11 +93,17 @@ impl WinbarServer {
_stream: &TcpStream,
) -> Result<()> {
match payload.message {
winbar::protocol::ServerMessage::UpdateWindow => {
ServerMessage::Shutdown => {
ctx.sender().send(WinbarAction::Shutdown)?;
}
ServerMessage::UpdateWindow => {
ctx.sender().send(WinbarAction::UpdateWindow)?;
}
winbar::protocol::ServerMessage::Shutdown => {
ctx.sender().send(WinbarAction::Shutdown)?;
ServerMessage::ShowWindow => {
ctx.sender().send(WinbarAction::ShowWindow)?;
}
ServerMessage::HideWindow => {
ctx.sender().send(WinbarAction::HideWindow)?;
}
}

Expand Down
14 changes: 13 additions & 1 deletion winbar/src/windows_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use windows::{
},
GdiPlus::{GdiplusShutdown, GdiplusStartup, GdiplusStartupInput, Status},
},
UI::WindowsAndMessaging::{PostMessageW, WM_CLOSE},
UI::WindowsAndMessaging::{PostMessageW, ShowWindow, SW_HIDE, SW_SHOW, WM_CLOSE},
},
};

Expand Down Expand Up @@ -74,6 +74,18 @@ impl WindowsApi {
}
}

pub fn show_window(hwnd: HWND) {
unsafe {
let _ = ShowWindow(hwnd, SW_SHOW);
}
}

pub fn hide_window(hwnd: HWND) {
unsafe {
let _ = ShowWindow(hwnd, SW_HIDE);
}
}

#[instrument]
pub fn send_window_shutdown_msg(hwnd: HWND) {
if let Err(e) = unsafe { PostMessageW(hwnd, WM_CLOSE, WPARAM(0), LPARAM(0)) } {
Expand Down
1 change: 1 addition & 0 deletions winbarc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ edition = { workspace = true }
winbar = { path = "../winbar" }
clap = { version = "4.5.4", features = ["cargo", "derive"] }
tokio = { version = "1.37.0", features = ["rt-multi-thread", "sync", "macros"] }
powershell_script = { version = "1.1.0", features = ["core"] }
4 changes: 4 additions & 0 deletions winbarc/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ pub enum WinbarSubcommand {
Stop,
/// Sends a message to update the status bar window
UpdateWindow,
/// Sends a message to show the status bar
Show,
/// Sends a message to hide the status bar
Hide,
}
37 changes: 27 additions & 10 deletions winbarc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
process::{Command, Stdio},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
Expand Down Expand Up @@ -60,13 +59,13 @@ async fn main() {
return;
};

match Command::new("winbar.exe")
.args(["--config-path", path])
.args(["--port", &port.to_string()])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
{
let arguments = format!("--config-path {} --port {}", path, &port.to_string());
let script = format!(
"Start-Process winbar -ArgumentList '{}' -WindowStyle hidden",
arguments
);

match powershell_script::run(&script) {
Ok(_) => {
log!("Started winbar!");
}
Expand All @@ -82,7 +81,7 @@ async fn main() {
WinbarSubcommand::Stop => {
log!("Sending shutdown payload...");
send.send(WinbarServerPayload {
id: 100000,
id: 0,
message: ServerMessage::Shutdown,
})
.await
Expand All @@ -91,12 +90,30 @@ async fn main() {
WinbarSubcommand::UpdateWindow => {
log!("Sending update window payload...");
send.send(WinbarServerPayload {
id: 100000,
id: 0,
message: ServerMessage::UpdateWindow,
})
.await
.unwrap();
}
WinbarSubcommand::Show => {
log!("Sending show window payload...");
send.send(WinbarServerPayload {
id: 0,
message: ServerMessage::ShowWindow,
})
.await
.unwrap();
}
WinbarSubcommand::Hide => {
log!("Sending hide window payload...");
send.send(WinbarServerPayload {
id: 0,
message: ServerMessage::HideWindow,
})
.await
.unwrap();
}
}

tokio::select! {
Expand Down

0 comments on commit 6f023f2

Please sign in to comment.