Skip to content

Commit 2a46155

Browse files
committed
feat: force theme update on startup
1 parent f6eb7eb commit 2a46155

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

Modules/Feature/AppFeature/HelixView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public struct HelixView: View {
2222
WithViewStore(store, observe: { $0 }) { viewStore in
2323
TermView(store: store.scope(state: \.terminalState, action: HelixFeature.Action.terminal))
2424
.padding(4)
25-
.background(viewStore.backgroundColor?.toSwiftUI() ?? SwiftUI.Color.blue)
25+
.background(viewStore.backgroundColor?.toSwiftUI() ?? SwiftUI.Color.black)
2626
}
2727
}
2828
}

Modules/Feature/HelixFeature/HelixFeature.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ public struct HelixFeature: Reducer, Sendable {
6363

6464
let env = ["HELIX_RUNTIME": helixRoot.appendingPathComponent("runtime").path]
6565

66+
6667
return .run { [ipcManager] send in
6768
await send(.terminal(.startShell(args: shellArgs, env: env)))
6869
await ipcManager.start(inputPipeURL: inputPipeURL, outputPipeURL: outputPipeURL)
70+
Task { await ipcManager.sendMessage("force_theme_update:") }
6971

7072
for await message in await ipcManager.serverMessages {
7173
print(message)
@@ -74,10 +76,12 @@ public struct HelixFeature: Reducer, Sendable {
7476
switch command {
7577
case "fileChanged":
7678
await send(.fileChanged(URL(fileURLWithPath: rest.first!)))
79+
7780
case "themeChanged":
7881
// rest should contain #RGB value
7982
let rgb = Color(hexColorString: rest.first!)
8083
await send(.themeChanged(bgColor: rgb))
84+
8185
default:
8286
print("unknown command \(message)")
8387
}

Modules/Feature/HelixFeature/IPCManager.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ actor IPCManager {
6363
func sendMessage(_ message: String) {
6464
guard let inputPipeURL else { return }
6565

66-
Task {
67-
let fileHandle = try! FileHandle(forWritingTo: inputPipeURL)
68-
defer { try! fileHandle.close() }
66+
print("sending message \(message)")
6967

70-
let secData = message.data(using: .utf8)
71-
if let secData = secData {
72-
try! fileHandle.write(contentsOf: secData)
73-
}
68+
let fileHandle = try! FileHandle(forWritingTo: inputPipeURL)
69+
defer { try! fileHandle.close() }
70+
71+
let secData = message.data(using: .utf8)
72+
if let secData = secData {
73+
try! fileHandle.write(contentsOf: secData)
7474
}
7575
}
7676

helix/helix-term/src/application.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ impl Application<'_> {
497497
"" => {
498498
self.execute_typed_command(&*rest.join(" "))
499499
}
500+
"force_theme_update" => {
501+
self.editor.ipc_notify_theme_changed();
502+
}
500503
"exit" => return false,
501504
_ => self.editor.set_error(format!("unknown command {}", command)),
502505
}

helix/helix-view/src/editor.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,7 @@ impl Editor {
11641164
let last_theme = std::mem::replace(&mut self.theme, theme);
11651165
// only insert on first preview: this will be the last theme the user has saved
11661166
self.last_theme.get_or_insert(last_theme);
1167+
self.ipc_notify_theme_changed();
11671168
}
11681169
ThemeAction::Set => {
11691170
self.last_theme = None;
@@ -1844,18 +1845,24 @@ impl Editor {
18441845
pub fn ipc_notify_theme_changed(&self) {
18451846
unsafe {
18461847
if let Some(ipc) = IPC.clone() {
1847-
if let Some(bgStyle) = self.theme.try_get("ui.background") {
1848-
if let Some(bgColor) = bgStyle.bg {
1849-
let hexColor = Self::color_to_hex_string(bgColor);
1850-
tokio::spawn(async move {
1851-
let _ = ipc.send_output_event(format!("themeChanged: {}", hexColor)).await;
1852-
});
1853-
}
1848+
if let Some(hex_color) = self.get_background_color() {
1849+
tokio::spawn(async move {
1850+
let _ = ipc.send_output_event(format!("themeChanged: {}", hex_color)).await;
1851+
});
18541852
}
18551853
}
18561854
}
18571855
}
18581856

1857+
pub fn get_background_color(&self) -> Option<String> {
1858+
if let Some(bg_style) = self.theme.try_get("ui.background") {
1859+
if let Some(bg_color) = bg_style.bg {
1860+
return Some(Self::color_to_hex_string(bg_color));
1861+
}
1862+
}
1863+
None
1864+
}
1865+
18591866
fn color_to_hex_string(color: Color) -> String {
18601867
match color {
18611868
Color::Rgb(r, g, b) => format!("#{:02X}{:02X}{:02X}", r, g, b),

0 commit comments

Comments
 (0)