feat(settings): preferred browser for links and Google OAuth
Let the user pick an installed browser for external http(s) opens, including Connect Google, without changing the managed Teams Chrome session. Default remains the OS handler.
@@ -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,177 @@
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
if launch_program(&program, url).is_err() {
55
cx.open_url(url);
56
}
57
}
58
}
59
}
60
61
fn launch_program(program: &str, url: &str) -> std::io::Result<()> {
62
#[cfg(target_os = "macos")]
63
{
64
if looks_like_app_name(program) {
65
Command::new("open").args(["-a", program, url]).spawn()?;
66
return Ok(());
67
}
68
}
69
Command::new(program).arg(url).spawn()?;
70
Ok(())
71
}
72
73
#[cfg(target_os = "macos")]
74
fn looks_like_app_name(program: &str) -> bool {
75
!program.contains('/') && program.chars().any(|c| c.is_uppercase() || c == ' ')
76
}
77
78
pub fn available_choices() -> Vec<BrowserChoice> {
79
let mut choices = vec![BrowserChoice {
80
id: SYSTEM_VALUE.to_owned(),
81
label: SYSTEM_VALUE.to_owned(),
82
}];
83
for (id, label) in discover_browsers() {
84
if choices.iter().any(|choice| choice.id == id) {
85
continue;
86
}
87
choices.push(BrowserChoice { id, label });
88
}
89
choices
90
}
91
92
pub fn is_system_id(id: &str) -> bool {
93
id == SYSTEM_VALUE
94
}
95
96
fn discover_browsers() -> Vec<(String, String)> {
97
#[cfg(target_os = "linux")]
98
{
99
const CANDIDATES: [(&str, &str); 6] = [
100
("google-chrome", "Google Chrome"),
101
("google-chrome-stable", "Google Chrome"),
102
("chromium", "Chromium"),
103
("chromium-browser", "Chromium"),
104
("firefox", "Firefox"),
105
("brave-browser", "Brave"),
106
];
107
return CANDIDATES
108
.into_iter()
109
.filter(|(name, _)| which(name).is_some())
110
.map(|(name, label)| (name.to_owned(), label.to_owned()))
111
.collect();
112
}
113
#[cfg(target_os = "macos")]
114
{
115
let mut found = Vec::new();
116
const APPS: [(&str, &str); 4] = [
117
("Google Chrome", "Google Chrome"),
118
("Chromium", "Chromium"),
119
("Firefox", "Firefox"),
120
("Safari", "Safari"),
121
];
122
let mut roots = vec![PathBuf::from("/Applications")];
123
if let Some(home) = dirs_home() {
124
roots.push(home.join("Applications"));
125
}
126
for (app, label) in APPS {
127
if roots
128
.iter()
129
.any(|root| root.join(format!("{app}.app")).is_dir())
130
{
131
found.push((app.to_owned(), label.to_owned()));
132
}
133
}
134
return found;
135
}
136
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
137
{
138
Vec::new()
139
}
140
}
141
142
fn which(name: &str) -> Option<PathBuf> {
143
let path = std::env::var_os("PATH")?;
144
for dir in std::env::split_paths(&path) {
145
let candidate = dir.join(name);
146
if is_executable(&candidate) {
147
return Some(candidate);
148
}
149
}
150
None
151
}
152
153
fn is_executable(path: &Path) -> bool {
154
path.is_file()
155
}
156
157
#[cfg(target_os = "macos")]
158
fn dirs_home() -> Option<PathBuf> {
159
std::env::var_os("HOME").map(PathBuf::from)
160
}
161
162
#[cfg(test)]
163
mod tests {
164
use super::{ExternalBrowser, SYSTEM_VALUE, load, save};
165
use store::Store;
166
167
#[test]
168
fn defaults_to_system_and_round_trips() {
169
let store = Store::open_in_memory().unwrap();
170
assert_eq!(load(&store), ExternalBrowser::System);
171
save(&store, &ExternalBrowser::Program("firefox".into()));
172
assert_eq!(load(&store), ExternalBrowser::Program("firefox".into()));
173
save(&store, &ExternalBrowser::System);
174
assert_eq!(load(&store), ExternalBrowser::System);
175
assert_eq!(ExternalBrowser::System.id(), SYSTEM_VALUE);
176
}
177
}
@@ -0,0 +1,177 @@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
if launch_program(&program, url).is_err() {55
cx.open_url(url);56
}57
}58
}59
}60
61
fn launch_program(program: &str, url: &str) -> std::io::Result<()> {62
#[cfg(target_os = "macos")]63
{64
if looks_like_app_name(program) {65
Command::new("open").args(["-a", program, url]).spawn()?;66
return Ok(());67
}68
}69
Command::new(program).arg(url).spawn()?;70
Ok(())71
}72
73
#[cfg(target_os = "macos")]74
fn looks_like_app_name(program: &str) -> bool {75
!program.contains('/') && program.chars().any(|c| c.is_uppercase() || c == ' ')76
}77
78
pub fn available_choices() -> Vec<BrowserChoice> {79
let mut choices = vec![BrowserChoice {80
id: SYSTEM_VALUE.to_owned(),81
label: SYSTEM_VALUE.to_owned(),82
}];83
for (id, label) in discover_browsers() {84
if choices.iter().any(|choice| choice.id == id) {85
continue;86
}87
choices.push(BrowserChoice { id, label });88
}89
choices90
}91
92
pub fn is_system_id(id: &str) -> bool {93
id == SYSTEM_VALUE94
}95
96
fn discover_browsers() -> Vec<(String, String)> {97
#[cfg(target_os = "linux")]98
{99
const CANDIDATES: [(&str, &str); 6] = [100
("google-chrome", "Google Chrome"),101
("google-chrome-stable", "Google Chrome"),102
("chromium", "Chromium"),103
("chromium-browser", "Chromium"),104
("firefox", "Firefox"),105
("brave-browser", "Brave"),106
];107
return CANDIDATES108
.into_iter()109
.filter(|(name, _)| which(name).is_some())110
.map(|(name, label)| (name.to_owned(), label.to_owned()))111
.collect();112
}113
#[cfg(target_os = "macos")]114
{115
let mut found = Vec::new();116
const APPS: [(&str, &str); 4] = [117
("Google Chrome", "Google Chrome"),118
("Chromium", "Chromium"),119
("Firefox", "Firefox"),120
("Safari", "Safari"),121
];122
let mut roots = vec![PathBuf::from("/Applications")];123
if let Some(home) = dirs_home() {124
roots.push(home.join("Applications"));125
}126
for (app, label) in APPS {127
if roots128
.iter()129
.any(|root| root.join(format!("{app}.app")).is_dir())130
{131
found.push((app.to_owned(), label.to_owned()));132
}133
}134
return found;135
}136
#[cfg(not(any(target_os = "linux", target_os = "macos")))]137
{138
Vec::new()139
}140
}141
142
fn which(name: &str) -> Option<PathBuf> {143
let path = std::env::var_os("PATH")?;144
for dir in std::env::split_paths(&path) {145
let candidate = dir.join(name);146
if is_executable(&candidate) {147
return Some(candidate);148
}149
}150
None151
}152
153
fn is_executable(path: &Path) -> bool {154
path.is_file()155
}156
157
#[cfg(target_os = "macos")]158
fn dirs_home() -> Option<PathBuf> {159
std::env::var_os("HOME").map(PathBuf::from)160
}161
162
#[cfg(test)]163
mod tests {164
use super::{ExternalBrowser, SYSTEM_VALUE, load, save};165
use store::Store;166
167
#[test]168
fn defaults_to_system_and_round_trips() {169
let store = Store::open_in_memory().unwrap();170
assert_eq!(load(&store), ExternalBrowser::System);171
save(&store, &ExternalBrowser::Program("firefox".into()));172
assert_eq!(load(&store), ExternalBrowser::Program("firefox".into()));173
save(&store, &ExternalBrowser::System);174
assert_eq!(load(&store), ExternalBrowser::System);175
assert_eq!(ExternalBrowser::System.id(), SYSTEM_VALUE);176
}177
}@@ -5,7 +5,9 @@ pub mod agents;
5
5
pub mod app_state;
6
6
pub mod backend;
7
7
pub mod banner_image;
8
pub mod browser_sign_in;
8
9
pub mod calendar_accounts_actions;
10
pub mod external_browser;
9
11
pub mod call;
10
12
pub mod call_rows;
11
13
pub mod card_actions;
@@ -5,7 +5,9 @@ pub mod agents;5
pub mod app_state;5
pub mod app_state;6
pub mod backend;6
pub mod backend;7
pub mod banner_image;7
pub mod banner_image;8
pub mod browser_sign_in;8
pub mod calendar_accounts_actions;9
pub mod calendar_accounts_actions;10
pub mod external_browser;9
pub mod call;11
pub mod call;10
pub mod call_rows;12
pub mod call_rows;11
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() {