Merge pull request #29: feat(settings): preferred browser for links and Google OAuth
@@ -122,6 +122,9 @@ settings:
122
122
system:
123
123
en: SYSTEM
124
124
de: SYSTEM
125
browser:
126
en: BROWSER
127
de: BROWSER
125
128
compose_box:
126
129
en: COMPOSE BOX
127
130
de: EINGABEFELD
@@ -186,6 +189,15 @@ settings:
186
189
never_translate:
187
190
en: Never translate messages in these languages
188
191
de: Nachrichten in diesen Sprachen nie übersetzen
192
external_browser:
193
en: Open links and Google sign-in with
194
de: Links und Google-Anmeldung öffnen mit
195
external_browser_detail:
196
en: Does not change the Teams session browser (Chrome/Chromium is required for that).
197
de: Ändert nicht den Browser der Teams-Sitzung (dafür bleibt Chrome/Chromium nötig).
198
external_browser_system:
199
en: System default
200
de: Systemstandard
189
201
notifications:
190
202
flash_taskbar:
191
203
en: Flash taskbar when notifications are received
@@ -122,6 +122,9 @@ settings:122
system:122
system:123
en: SYSTEM123
en: SYSTEM124
de: SYSTEM124
de: SYSTEM125
browser:126
en: BROWSER127
de: BROWSER125
compose_box:128
compose_box:126
en: COMPOSE BOX129
en: COMPOSE BOX127
de: EINGABEFELD130
de: EINGABEFELD@@ -186,6 +189,15 @@ settings:186
never_translate:189
never_translate:187
en: Never translate messages in these languages190
en: Never translate messages in these languages188
de: Nachrichten in diesen Sprachen nie übersetzen191
de: Nachrichten in diesen Sprachen nie übersetzen192
external_browser:193
en: Open links and Google sign-in with194
de: Links und Google-Anmeldung öffnen mit195
external_browser_detail:196
en: Does not change the Teams session browser (Chrome/Chromium is required for that).197
de: Ändert nicht den Browser der Teams-Sitzung (dafür bleibt Chrome/Chromium nötig).198
external_browser_system:199
en: System default200
de: Systemstandard189
notifications:201
notifications:190
flash_taskbar:202
flash_taskbar:191
en: Flash taskbar when notifications are received203
en: Flash taskbar when notifications are received@@ -118,7 +118,11 @@ impl AppState {
118
118
return;
119
119
}
120
120
};
121
this.update(cx, |_, cx| cx.open_url(&authorize_url)).ok();
121
this
122
.update(cx, |state, cx| {
123
crate::external_browser::open_url(cx, &state.store, &authorize_url);
124
})
125
.ok();
122
126
let finished = runtime::spawn(async move {
123
127
finish_google_connect(oauth, pending, listener, store).await
124
128
});
@@ -118,7 +118,11 @@ impl AppState {118
return;118
return;119
}119
}120
};120
};121
this.update(cx, |_, cx| cx.open_url(&authorize_url)).ok();121
this122
.update(cx, |state, cx| {123
crate::external_browser::open_url(cx, &state.store, &authorize_url);124
})125
.ok();122
let finished = runtime::spawn(async move {126
let finished = runtime::spawn(async move {123
finish_google_connect(oauth, pending, listener, store).await127
finish_google_connect(oauth, pending, listener, store).await124
});128
});@@ -0,0 +1,178 @@
1
//! Preferred browser for external opens (Google OAuth, links).
2
//!
3
//! The managed Chrome session for Microsoft Teams login is unchanged (ADR 0003).
4
5
use std::path::{Path, PathBuf};
6
use std::process::Command;
7
8
use gpui_kit::App;
9
use store::Store;
10
11
const META_KEY: &str = "external_browser";
12
const SYSTEM_VALUE: &str = "system";
13
14
#[derive(Debug, Clone, PartialEq, Eq)]
15
pub enum ExternalBrowser {
16
System,
17
Program(String),
18
}
19
20
#[derive(Debug, Clone, PartialEq, Eq)]
21
pub struct BrowserChoice {
22
pub id: String,
23
pub label: String,
24
}
25
26
impl ExternalBrowser {
27
pub fn id(&self) -> &str {
28
match self {
29
ExternalBrowser::System => SYSTEM_VALUE,
30
ExternalBrowser::Program(program) => program.as_str(),
31
}
32
}
33
}
34
35
pub fn load(store: &Store) -> ExternalBrowser {
36
match store.meta(META_KEY).ok().flatten().as_deref() {
37
None | Some(SYSTEM_VALUE) | Some("") => ExternalBrowser::System,
38
Some(program) => ExternalBrowser::Program(program.to_owned()),
39
}
40
}
41
42
pub fn save(store: &Store, browser: &ExternalBrowser) {
43
let value = match browser {
44
ExternalBrowser::System => SYSTEM_VALUE,
45
ExternalBrowser::Program(program) => program.as_str(),
46
};
47
let _ = store.set_meta(META_KEY, value);
48
}
49
50
pub fn open_url(cx: &mut App, store: &Store, url: &str) {
51
match load(store) {
52
ExternalBrowser::System => cx.open_url(url),
53
ExternalBrowser::Program(program) => {
54
let known = available_choices().iter().any(|choice| choice.id == program);
55
if !known || launch_program(&program, url).is_err() {
56
cx.open_url(url);
57
}
58
}
59
}
60
}
61
62
fn launch_program(program: &str, url: &str) -> std::io::Result<()> {
63
#[cfg(target_os = "macos")]
64
{
65
if looks_like_app_name(program) {
66
Command::new("open").args(["-a", program, url]).spawn()?;
67
return Ok(());
68
}
69
}
70
Command::new(program).arg(url).spawn()?;
71
Ok(())
72
}
73
74
#[cfg(target_os = "macos")]
75
fn looks_like_app_name(program: &str) -> bool {
76
!program.contains('/') && program.chars().any(|c| c.is_uppercase() || c == ' ')
77
}
78
79
pub fn available_choices() -> Vec<BrowserChoice> {
80
let mut choices = vec![BrowserChoice {
81
id: SYSTEM_VALUE.to_owned(),
82
label: SYSTEM_VALUE.to_owned(),
83
}];
84
for (id, label) in discover_browsers() {
85
if choices.iter().any(|choice| choice.id == id) {
86
continue;
87
}
88
choices.push(BrowserChoice { id, label });
89
}
90
choices
91
}
92
93
pub fn is_system_id(id: &str) -> bool {
94
id == SYSTEM_VALUE
95
}
96
97
fn discover_browsers() -> Vec<(String, String)> {
98
#[cfg(target_os = "linux")]
99
{
100
const CANDIDATES: [(&str, &str); 6] = [
101
("google-chrome", "Google Chrome"),
102
("google-chrome-stable", "Google Chrome"),
103
("chromium", "Chromium"),
104
("chromium-browser", "Chromium"),
105
("firefox", "Firefox"),
106
("brave-browser", "Brave"),
107
];
108
return CANDIDATES
109
.into_iter()
110
.filter(|(name, _)| which(name).is_some())
111
.map(|(name, label)| (name.to_owned(), label.to_owned()))
112
.collect();
113
}
114
#[cfg(target_os = "macos")]
115
{
116
let mut found = Vec::new();
117
const APPS: [(&str, &str); 4] = [
118
("Google Chrome", "Google Chrome"),
119
("Chromium", "Chromium"),
120
("Firefox", "Firefox"),
121
("Safari", "Safari"),
122
];
123
let mut roots = vec![PathBuf::from("/Applications")];
124
if let Some(home) = dirs_home() {
125
roots.push(home.join("Applications"));
126
}
127
for (app, label) in APPS {
128
if roots
129
.iter()
130
.any(|root| root.join(format!("{app}.app")).is_dir())
131
{
132
found.push((app.to_owned(), label.to_owned()));
133
}
134
}
135
return found;
136
}
137
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
138
{
139
Vec::new()
140
}
141
}
142
143
fn which(name: &str) -> Option<PathBuf> {
144
let path = std::env::var_os("PATH")?;
145
for dir in std::env::split_paths(&path) {
146
let candidate = dir.join(name);
147
if is_executable(&candidate) {
148
return Some(candidate);
149
}
150
}
151
None
152
}
153
154
fn is_executable(path: &Path) -> bool {
155
path.is_file()
156
}
157
158
#[cfg(target_os = "macos")]
159
fn dirs_home() -> Option<PathBuf> {
160
std::env::var_os("HOME").map(PathBuf::from)
161
}
162
163
#[cfg(test)]
164
mod tests {
165
use super::{ExternalBrowser, SYSTEM_VALUE, load, save};
166
use store::Store;
167
168
#[test]
169
fn defaults_to_system_and_round_trips() {
170
let store = Store::open_in_memory().unwrap();
171
assert_eq!(load(&store), ExternalBrowser::System);
172
save(&store, &ExternalBrowser::Program("firefox".into()));
173
assert_eq!(load(&store), ExternalBrowser::Program("firefox".into()));
174
save(&store, &ExternalBrowser::System);
175
assert_eq!(load(&store), ExternalBrowser::System);
176
assert_eq!(ExternalBrowser::System.id(), SYSTEM_VALUE);
177
}
178
}
@@ -0,0 +1,178 @@1
//! Preferred browser for external opens (Google OAuth, links).2
//!3
//! The managed Chrome session for Microsoft Teams login is unchanged (ADR 0003).4
5
use std::path::{Path, PathBuf};6
use std::process::Command;7
8
use gpui_kit::App;9
use store::Store;10
11
const META_KEY: &str = "external_browser";12
const SYSTEM_VALUE: &str = "system";13
14
#[derive(Debug, Clone, PartialEq, Eq)]15
pub enum ExternalBrowser {16
System,17
Program(String),18
}19
20
#[derive(Debug, Clone, PartialEq, Eq)]21
pub struct BrowserChoice {22
pub id: String,23
pub label: String,24
}25
26
impl ExternalBrowser {27
pub fn id(&self) -> &str {28
match self {29
ExternalBrowser::System => SYSTEM_VALUE,30
ExternalBrowser::Program(program) => program.as_str(),31
}32
}33
}34
35
pub fn load(store: &Store) -> ExternalBrowser {36
match store.meta(META_KEY).ok().flatten().as_deref() {37
None | Some(SYSTEM_VALUE) | Some("") => ExternalBrowser::System,38
Some(program) => ExternalBrowser::Program(program.to_owned()),39
}40
}41
42
pub fn save(store: &Store, browser: &ExternalBrowser) {43
let value = match browser {44
ExternalBrowser::System => SYSTEM_VALUE,45
ExternalBrowser::Program(program) => program.as_str(),46
};47
let _ = store.set_meta(META_KEY, value);48
}49
50
pub fn open_url(cx: &mut App, store: &Store, url: &str) {51
match load(store) {52
ExternalBrowser::System => cx.open_url(url),53
ExternalBrowser::Program(program) => {54
let known = available_choices().iter().any(|choice| choice.id == program);55
if !known || launch_program(&program, url).is_err() {56
cx.open_url(url);57
}58
}59
}60
}61
62
fn launch_program(program: &str, url: &str) -> std::io::Result<()> {63
#[cfg(target_os = "macos")]64
{65
if looks_like_app_name(program) {66
Command::new("open").args(["-a", program, url]).spawn()?;67
return Ok(());68
}69
}70
Command::new(program).arg(url).spawn()?;71
Ok(())72
}73
74
#[cfg(target_os = "macos")]75
fn looks_like_app_name(program: &str) -> bool {76
!program.contains('/') && program.chars().any(|c| c.is_uppercase() || c == ' ')77
}78
79
pub fn available_choices() -> Vec<BrowserChoice> {80
let mut choices = vec![BrowserChoice {81
id: SYSTEM_VALUE.to_owned(),82
label: SYSTEM_VALUE.to_owned(),83
}];84
for (id, label) in discover_browsers() {85
if choices.iter().any(|choice| choice.id == id) {86
continue;87
}88
choices.push(BrowserChoice { id, label });89
}90
choices91
}92
93
pub fn is_system_id(id: &str) -> bool {94
id == SYSTEM_VALUE95
}96
97
fn discover_browsers() -> Vec<(String, String)> {98
#[cfg(target_os = "linux")]99
{100
const CANDIDATES: [(&str, &str); 6] = [101
("google-chrome", "Google Chrome"),102
("google-chrome-stable", "Google Chrome"),103
("chromium", "Chromium"),104
("chromium-browser", "Chromium"),105
("firefox", "Firefox"),106
("brave-browser", "Brave"),107
];108
return CANDIDATES109
.into_iter()110
.filter(|(name, _)| which(name).is_some())111
.map(|(name, label)| (name.to_owned(), label.to_owned()))112
.collect();113
}114
#[cfg(target_os = "macos")]115
{116
let mut found = Vec::new();117
const APPS: [(&str, &str); 4] = [118
("Google Chrome", "Google Chrome"),119
("Chromium", "Chromium"),120
("Firefox", "Firefox"),121
("Safari", "Safari"),122
];123
let mut roots = vec![PathBuf::from("/Applications")];124
if let Some(home) = dirs_home() {125
roots.push(home.join("Applications"));126
}127
for (app, label) in APPS {128
if roots129
.iter()130
.any(|root| root.join(format!("{app}.app")).is_dir())131
{132
found.push((app.to_owned(), label.to_owned()));133
}134
}135
return found;136
}137
#[cfg(not(any(target_os = "linux", target_os = "macos")))]138
{139
Vec::new()140
}141
}142
143
fn which(name: &str) -> Option<PathBuf> {144
let path = std::env::var_os("PATH")?;145
for dir in std::env::split_paths(&path) {146
let candidate = dir.join(name);147
if is_executable(&candidate) {148
return Some(candidate);149
}150
}151
None152
}153
154
fn is_executable(path: &Path) -> bool {155
path.is_file()156
}157
158
#[cfg(target_os = "macos")]159
fn dirs_home() -> Option<PathBuf> {160
std::env::var_os("HOME").map(PathBuf::from)161
}162
163
#[cfg(test)]164
mod tests {165
use super::{ExternalBrowser, SYSTEM_VALUE, load, save};166
use store::Store;167
168
#[test]169
fn defaults_to_system_and_round_trips() {170
let store = Store::open_in_memory().unwrap();171
assert_eq!(load(&store), ExternalBrowser::System);172
save(&store, &ExternalBrowser::Program("firefox".into()));173
assert_eq!(load(&store), ExternalBrowser::Program("firefox".into()));174
save(&store, &ExternalBrowser::System);175
assert_eq!(load(&store), ExternalBrowser::System);176
assert_eq!(ExternalBrowser::System.id(), SYSTEM_VALUE);177
}178
}@@ -7,6 +7,7 @@ pub mod backend;
7
7
pub mod banner_image;
8
8
pub mod browser_sign_in;
9
9
pub mod calendar_accounts_actions;
10
pub mod external_browser;
10
11
pub mod call;
11
12
pub mod call_rows;
12
13
pub mod card_actions;
@@ -7,6 +7,7 @@ pub mod backend;7
pub mod banner_image;7
pub mod banner_image;8
pub mod browser_sign_in;8
pub mod browser_sign_in;9
pub mod calendar_accounts_actions;9
pub mod calendar_accounts_actions;10
pub mod external_browser;10
pub mod call;11
pub mod call;11
pub mod call_rows;12
pub mod call_rows;12
pub mod card_actions;13
pub mod card_actions;@@ -78,6 +78,7 @@ pub fn render(center: &Entity<NotificationCenter>, app: &Entity<AppState>, cx: &
78
78
})
79
79
}),
80
80
)
81
.children(external_browser_rows(app, cx))
81
82
.child(caption(tr("settings.caption.compose_box")))
82
83
.child(account_select(
83
84
app,
@@ -94,6 +95,68 @@ pub fn render(center: &Entity<NotificationCenter>, app: &Entity<AppState>, cx: &
94
95
.children(translation_rows(app, cx))
95
96
}
96
97
98
fn external_browser_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> {
99
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
100
{
101
let _ = (app, cx);
102
return Vec::new();
103
}
104
#[cfg(any(target_os = "linux", target_os = "macos"))]
105
{
106
use crate::external_browser::{self, ExternalBrowser};
107
108
let selected = external_browser::load(&app.read(cx).store);
109
let current_label = external_browser::available_choices()
110
.into_iter()
111
.find(|choice| choice.id == selected.id())
112
.map(|choice| {
113
if external_browser::is_system_id(&choice.id) {
114
tr("settings.general.external_browser_system")
115
} else {
116
choice.label
117
}
118
})
119
.unwrap_or_else(|| tr("settings.general.external_browser_system"));
120
let choices: Vec<Choice> = external_browser::available_choices()
121
.into_iter()
122
.map(|choice| {
123
let label = if external_browser::is_system_id(&choice.id) {
124
tr("settings.general.external_browser_system")
125
} else {
126
choice.label.clone()
127
};
128
let is_selected = selected.id() == choice.id;
129
let (app, id) = (app.clone(), choice.id);
130
Choice::new(label, is_selected, move |cx| {
131
app.update(cx, |state, cx| {
132
let browser = if external_browser::is_system_id(&id) {
133
ExternalBrowser::System
134
} else {
135
ExternalBrowser::Program(id.clone())
136
};
137
external_browser::save(&state.store, &browser);
138
cx.notify();
139
});
140
})
141
})
142
.collect();
143
vec![
144
caption(tr("settings.caption.browser")).into_any_element(),
145
control_row(
146
tr("settings.general.external_browser"),
147
tr("settings.general.external_browser_detail"),
148
select(
149
"settings-external-browser",
150
current_label,
151
false,
152
choices,
153
),
154
)
155
.into_any_element(),
156
]
157
}
158
}
159
97
160
fn translation_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> {
98
161
let state = app.read(cx);
99
162
if state.translation.settings.is_none() {
@@ -78,6 +78,7 @@ pub fn render(center: &Entity<NotificationCenter>, app: &Entity<AppState>, cx: &78
})78
})79
}),79
}),80
)80
)81
.children(external_browser_rows(app, cx))81
.child(caption(tr("settings.caption.compose_box")))82
.child(caption(tr("settings.caption.compose_box")))82
.child(account_select(83
.child(account_select(83
app,84
app,@@ -94,6 +95,68 @@ pub fn render(center: &Entity<NotificationCenter>, app: &Entity<AppState>, cx: &94
.children(translation_rows(app, cx))95
.children(translation_rows(app, cx))95
}96
}96
97
98
fn external_browser_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> {99
#[cfg(not(any(target_os = "linux", target_os = "macos")))]100
{101
let _ = (app, cx);102
return Vec::new();103
}104
#[cfg(any(target_os = "linux", target_os = "macos"))]105
{106
use crate::external_browser::{self, ExternalBrowser};107
108
let selected = external_browser::load(&app.read(cx).store);109
let current_label = external_browser::available_choices()110
.into_iter()111
.find(|choice| choice.id == selected.id())112
.map(|choice| {113
if external_browser::is_system_id(&choice.id) {114
tr("settings.general.external_browser_system")115
} else {116
choice.label117
}118
})119
.unwrap_or_else(|| tr("settings.general.external_browser_system"));120
let choices: Vec<Choice> = external_browser::available_choices()121
.into_iter()122
.map(|choice| {123
let label = if external_browser::is_system_id(&choice.id) {124
tr("settings.general.external_browser_system")125
} else {126
choice.label.clone()127
};128
let is_selected = selected.id() == choice.id;129
let (app, id) = (app.clone(), choice.id);130
Choice::new(label, is_selected, move |cx| {131
app.update(cx, |state, cx| {132
let browser = if external_browser::is_system_id(&id) {133
ExternalBrowser::System134
} else {135
ExternalBrowser::Program(id.clone())136
};137
external_browser::save(&state.store, &browser);138
cx.notify();139
});140
})141
})142
.collect();143
vec![144
caption(tr("settings.caption.browser")).into_any_element(),145
control_row(146
tr("settings.general.external_browser"),147
tr("settings.general.external_browser_detail"),148
select(149
"settings-external-browser",150
current_label,151
false,152
choices,153
),154
)155
.into_any_element(),156
]157
}158
}159
97
fn translation_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> {160
fn translation_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> {98
let state = app.read(cx);161
let state = app.read(cx);99
if state.translation.settings.is_none() {162
if state.translation.settings.is_none() {