fix(channels): website tabs open in the browser like Teams, decode tab names
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -1,271 +0,0 @@
1
use gpui_kit::component::{
2
button::{Button, ButtonVariants as _},
3
h_flex, v_flex,
4
};
5
use gpui_kit::*;
6
use tokio::sync::mpsc::UnboundedReceiver;
7
8
use crate::theme;
9
10
#[cfg(windows)]
11
mod native_windows;
12
#[cfg(windows)]
13
use native_windows as native;
14
#[cfg(not(windows))]
15
mod native_stub;
16
#[cfg(not(windows))]
17
use native_stub as native;
18
19
#[cfg(windows)]
20
pub use native::configure;
21
pub use native::{Native, NativeEvent, available};
22
23
const BAR_HEIGHT: f32 = 28.;
24
const BAR_TEXT_SIZE: f32 = 11.5;
25
const OPEN_LABEL: &str = "Open in browser \u{2197}";
26
27
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
28
pub struct Overlays {
29
pub panels: bool,
30
}
31
32
impl Global for Overlays {}
33
34
impl Overlays {
35
pub fn read(cx: &App) -> Self {
36
cx.try_global::<Overlays>().copied().unwrap_or_default()
37
}
38
39
pub fn update(cx: &mut App, change: impl FnOnce(&mut Overlays)) {
40
let mut next = Overlays::read(cx);
41
change(&mut next);
42
if next != Overlays::read(cx) {
43
cx.set_global(next);
44
}
45
}
46
}
47
48
pub struct RootFocus(pub FocusHandle);
49
50
impl Global for RootFocus {}
51
52
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53
pub enum FocusSpot {
54
Nowhere,
55
Page,
56
Elsewhere,
57
}
58
59
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60
pub struct Visibility {
61
pub overlays: Overlays,
62
pub has_area: bool,
63
pub focus: FocusSpot,
64
}
65
66
pub fn embed_visible(visibility: Visibility) -> bool {
67
visibility.has_area && !visibility.overlays.panels && visibility.focus != FocusSpot::Elsewhere
68
}
69
70
pub fn focus_spot<T: PartialEq>(focused: Option<T>, inside_pane: bool, page: &[T]) -> FocusSpot {
71
match focused {
72
None => FocusSpot::Nowhere,
73
Some(_) if inside_pane => FocusSpot::Page,
74
Some(handle) if page.contains(&handle) => FocusSpot::Page,
75
Some(_) => FocusSpot::Elsewhere,
76
}
77
}
78
79
pub fn url_bar_text(url: &str) -> String {
80
let shown = url.strip_prefix("https://").unwrap_or(url);
81
shown.strip_suffix('/').unwrap_or(shown).to_owned()
82
}
83
84
pub enum EmbeddedWebEvent {
85
Closed { start_url: String },
86
}
87
88
pub struct EmbeddedWeb {
89
tab_id: String,
90
start_url: String,
91
url: String,
92
native: Native,
93
focus_handle: FocusHandle,
94
container_focus: FocusHandle,
95
_events: Task<()>,
96
}
97
98
impl EventEmitter<EmbeddedWebEvent> for EmbeddedWeb {}
99
100
impl EmbeddedWeb {
101
pub fn new(
102
tab_id: String,
103
start_url: String,
104
native: Native,
105
mut events: UnboundedReceiver<NativeEvent>,
106
container_focus: FocusHandle,
107
cx: &mut Context<Self>,
108
) -> Self {
109
let task = cx.spawn(async move |this, cx| {
110
while let Some(event) = events.recv().await {
111
let handled = this.update(cx, |this, cx| this.on_event(event, cx));
112
if handled.is_err() {
113
break;
114
}
115
}
116
});
117
EmbeddedWeb {
118
tab_id,
119
url: start_url.clone(),
120
start_url,
121
native,
122
focus_handle: cx.focus_handle(),
123
container_focus,
124
_events: task,
125
}
126
}
127
128
pub fn tab_id(&self) -> &str {
129
&self.tab_id
130
}
131
132
fn on_event(&mut self, event: NativeEvent, cx: &mut Context<Self>) {
133
match event {
134
NativeEvent::Navigated(url) => {
135
self.url = url;
136
cx.notify();
137
}
138
NativeEvent::NewWindow(url) => cx.open_url(&url),
139
NativeEvent::Loaded => {}
140
NativeEvent::Closed => cx.emit(EmbeddedWebEvent::Closed {
141
start_url: self.start_url.clone(),
142
}),
143
}
144
}
145
}
146
147
impl Drop for EmbeddedWeb {
148
fn drop(&mut self) {
149
self.native.close();
150
}
151
}
152
153
impl Render for EmbeddedWeb {
154
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
155
let native = self.native.clone();
156
let pane_focus = self.focus_handle.clone();
157
let container_focus = self.container_focus.clone();
158
let open_url = self.url.clone();
159
v_flex()
160
.size_full()
161
.track_focus(&self.focus_handle)
162
.child(
163
h_flex()
164
.h(px(BAR_HEIGHT))
165
.flex_none()
166
.w_full()
167
.px(px(12.))
168
.gap(px(8.))
169
.items_center()
170
.border_b_1()
171
.border_color(theme::border())
172
.child(
173
div()
174
.flex_1()
175
.min_w_0()
176
.overflow_hidden()
177
.text_ellipsis()
178
.whitespace_nowrap()
179
.text_size(px(BAR_TEXT_SIZE))
180
.text_color(theme::text_muted())
181
.child(url_bar_text(&self.url)),
182
)
183
.child(
184
Button::new("embedded-web-open")
185
.ghost()
186
.compact()
187
.label(OPEN_LABEL)
188
.on_click(move |_, _, cx| cx.open_url(&open_url)),
189
),
190
)
191
.child(
192
div()
193
.flex_1()
194
.min_h_0()
195
.w_full()
196
.bg(theme::background())
197
.child(
198
canvas(
199
move |bounds, window, cx| {
200
let mut page = vec![container_focus.clone()];
201
page.extend(
202
cx.try_global::<RootFocus>().map(|root| root.0.clone()),
203
);
204
let visible = embed_visible(Visibility {
205
overlays: Overlays::read(cx),
206
has_area: bounds.size.width > px(0.)
207
&& bounds.size.height > px(0.),
208
focus: focus_spot(
209
window.focused(cx),
210
pane_focus.contains_focused(window, cx),
211
&page,
212
),
213
});
214
native.place(bounds, window.scale_factor(), visible);
215
},
216
|_, _, _, _| {},
217
)
218
.size_full(),
219
),
220
)
221
}
222
}
223
224
#[cfg(test)]
225
mod tests {
226
use super::{FocusSpot, Overlays, Visibility, embed_visible, focus_spot, url_bar_text};
227
228
fn visibility(panels: bool, focus: FocusSpot, has_area: bool) -> Visibility {
229
Visibility {
230
overlays: Overlays { panels },
231
has_area,
232
focus,
233
}
234
}
235
236
#[test]
237
fn the_page_shows_only_with_room_and_no_panel() {
238
assert!(embed_visible(visibility(false, FocusSpot::Page, true)));
239
assert!(!embed_visible(visibility(true, FocusSpot::Page, true)));
240
assert!(!embed_visible(visibility(false, FocusSpot::Page, false)));
241
}
242
243
#[test]
244
fn focus_outside_the_page_hides_it() {
245
assert!(!embed_visible(visibility(
246
false,
247
FocusSpot::Elsewhere,
248
true
249
)));
250
assert!(embed_visible(visibility(false, FocusSpot::Nowhere, true)));
251
}
252
253
#[test]
254
fn focus_is_classified_against_the_page_handles() {
255
assert_eq!(focus_spot(None, false, &[1, 2]), FocusSpot::Nowhere);
256
assert_eq!(focus_spot(Some(2), false, &[1, 2]), FocusSpot::Page);
257
assert_eq!(focus_spot(Some(9), true, &[1, 2]), FocusSpot::Page);
258
assert_eq!(focus_spot(Some(9), false, &[1, 2]), FocusSpot::Elsewhere);
259
assert_eq!(focus_spot(Some(9), false, &[]), FocusSpot::Elsewhere);
260
}
261
262
#[test]
263
fn the_url_bar_drops_the_scheme_and_a_trailing_slash() {
264
assert_eq!(url_bar_text("https://docs.example/"), "docs.example");
265
assert_eq!(
266
url_bar_text("https://docs.example/a/b?c=1"),
267
"docs.example/a/b?c=1"
268
);
269
assert_eq!(url_bar_text("http://plain.example"), "http://plain.example");
270
}
271
}
@@ -1,271 +0,0 @@1
use gpui_kit::component::{2
button::{Button, ButtonVariants as _},3
h_flex, v_flex,4
};5
use gpui_kit::*;6
use tokio::sync::mpsc::UnboundedReceiver;7
8
use crate::theme;9
10
#[cfg(windows)]11
mod native_windows;12
#[cfg(windows)]13
use native_windows as native;14
#[cfg(not(windows))]15
mod native_stub;16
#[cfg(not(windows))]17
use native_stub as native;18
19
#[cfg(windows)]20
pub use native::configure;21
pub use native::{Native, NativeEvent, available};22
23
const BAR_HEIGHT: f32 = 28.;24
const BAR_TEXT_SIZE: f32 = 11.5;25
const OPEN_LABEL: &str = "Open in browser \u{2197}";26
27
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]28
pub struct Overlays {29
pub panels: bool,30
}31
32
impl Global for Overlays {}33
34
impl Overlays {35
pub fn read(cx: &App) -> Self {36
cx.try_global::<Overlays>().copied().unwrap_or_default()37
}38
39
pub fn update(cx: &mut App, change: impl FnOnce(&mut Overlays)) {40
let mut next = Overlays::read(cx);41
change(&mut next);42
if next != Overlays::read(cx) {43
cx.set_global(next);44
}45
}46
}47
48
pub struct RootFocus(pub FocusHandle);49
50
impl Global for RootFocus {}51
52
#[derive(Debug, Clone, Copy, PartialEq, Eq)]53
pub enum FocusSpot {54
Nowhere,55
Page,56
Elsewhere,57
}58
59
#[derive(Debug, Clone, Copy, PartialEq, Eq)]60
pub struct Visibility {61
pub overlays: Overlays,62
pub has_area: bool,63
pub focus: FocusSpot,64
}65
66
pub fn embed_visible(visibility: Visibility) -> bool {67
visibility.has_area && !visibility.overlays.panels && visibility.focus != FocusSpot::Elsewhere68
}69
70
pub fn focus_spot<T: PartialEq>(focused: Option<T>, inside_pane: bool, page: &[T]) -> FocusSpot {71
match focused {72
None => FocusSpot::Nowhere,73
Some(_) if inside_pane => FocusSpot::Page,74
Some(handle) if page.contains(&handle) => FocusSpot::Page,75
Some(_) => FocusSpot::Elsewhere,76
}77
}78
79
pub fn url_bar_text(url: &str) -> String {80
let shown = url.strip_prefix("https://").unwrap_or(url);81
shown.strip_suffix('/').unwrap_or(shown).to_owned()82
}83
84
pub enum EmbeddedWebEvent {85
Closed { start_url: String },86
}87
88
pub struct EmbeddedWeb {89
tab_id: String,90
start_url: String,91
url: String,92
native: Native,93
focus_handle: FocusHandle,94
container_focus: FocusHandle,95
_events: Task<()>,96
}97
98
impl EventEmitter<EmbeddedWebEvent> for EmbeddedWeb {}99
100
impl EmbeddedWeb {101
pub fn new(102
tab_id: String,103
start_url: String,104
native: Native,105
mut events: UnboundedReceiver<NativeEvent>,106
container_focus: FocusHandle,107
cx: &mut Context<Self>,108
) -> Self {109
let task = cx.spawn(async move |this, cx| {110
while let Some(event) = events.recv().await {111
let handled = this.update(cx, |this, cx| this.on_event(event, cx));112
if handled.is_err() {113
break;114
}115
}116
});117
EmbeddedWeb {118
tab_id,119
url: start_url.clone(),120
start_url,121
native,122
focus_handle: cx.focus_handle(),123
container_focus,124
_events: task,125
}126
}127
128
pub fn tab_id(&self) -> &str {129
&self.tab_id130
}131
132
fn on_event(&mut self, event: NativeEvent, cx: &mut Context<Self>) {133
match event {134
NativeEvent::Navigated(url) => {135
self.url = url;136
cx.notify();137
}138
NativeEvent::NewWindow(url) => cx.open_url(&url),139
NativeEvent::Loaded => {}140
NativeEvent::Closed => cx.emit(EmbeddedWebEvent::Closed {141
start_url: self.start_url.clone(),142
}),143
}144
}145
}146
147
impl Drop for EmbeddedWeb {148
fn drop(&mut self) {149
self.native.close();150
}151
}152
153
impl Render for EmbeddedWeb {154
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {155
let native = self.native.clone();156
let pane_focus = self.focus_handle.clone();157
let container_focus = self.container_focus.clone();158
let open_url = self.url.clone();159
v_flex()160
.size_full()161
.track_focus(&self.focus_handle)162
.child(163
h_flex()164
.h(px(BAR_HEIGHT))165
.flex_none()166
.w_full()167
.px(px(12.))168
.gap(px(8.))169
.items_center()170
.border_b_1()171
.border_color(theme::border())172
.child(173
div()174
.flex_1()175
.min_w_0()176
.overflow_hidden()177
.text_ellipsis()178
.whitespace_nowrap()179
.text_size(px(BAR_TEXT_SIZE))180
.text_color(theme::text_muted())181
.child(url_bar_text(&self.url)),182
)183
.child(184
Button::new("embedded-web-open")185
.ghost()186
.compact()187
.label(OPEN_LABEL)188
.on_click(move |_, _, cx| cx.open_url(&open_url)),189
),190
)191
.child(192
div()193
.flex_1()194
.min_h_0()195
.w_full()196
.bg(theme::background())197
.child(198
canvas(199
move |bounds, window, cx| {200
let mut page = vec![container_focus.clone()];201
page.extend(202
cx.try_global::<RootFocus>().map(|root| root.0.clone()),203
);204
let visible = embed_visible(Visibility {205
overlays: Overlays::read(cx),206
has_area: bounds.size.width > px(0.)207
&& bounds.size.height > px(0.),208
focus: focus_spot(209
window.focused(cx),210
pane_focus.contains_focused(window, cx),211
&page,212
),213
});214
native.place(bounds, window.scale_factor(), visible);215
},216
|_, _, _, _| {},217
)218
.size_full(),219
),220
)221
}222
}223
224
#[cfg(test)]225
mod tests {226
use super::{FocusSpot, Overlays, Visibility, embed_visible, focus_spot, url_bar_text};227
228
fn visibility(panels: bool, focus: FocusSpot, has_area: bool) -> Visibility {229
Visibility {230
overlays: Overlays { panels },231
has_area,232
focus,233
}234
}235
236
#[test]237
fn the_page_shows_only_with_room_and_no_panel() {238
assert!(embed_visible(visibility(false, FocusSpot::Page, true)));239
assert!(!embed_visible(visibility(true, FocusSpot::Page, true)));240
assert!(!embed_visible(visibility(false, FocusSpot::Page, false)));241
}242
243
#[test]244
fn focus_outside_the_page_hides_it() {245
assert!(!embed_visible(visibility(246
false,247
FocusSpot::Elsewhere,248
true249
)));250
assert!(embed_visible(visibility(false, FocusSpot::Nowhere, true)));251
}252
253
#[test]254
fn focus_is_classified_against_the_page_handles() {255
assert_eq!(focus_spot(None, false, &[1, 2]), FocusSpot::Nowhere);256
assert_eq!(focus_spot(Some(2), false, &[1, 2]), FocusSpot::Page);257
assert_eq!(focus_spot(Some(9), true, &[1, 2]), FocusSpot::Page);258
assert_eq!(focus_spot(Some(9), false, &[1, 2]), FocusSpot::Elsewhere);259
assert_eq!(focus_spot(Some(9), false, &[]), FocusSpot::Elsewhere);260
}261
262
#[test]263
fn the_url_bar_drops_the_scheme_and_a_trailing_slash() {264
assert_eq!(url_bar_text("https://docs.example/"), "docs.example");265
assert_eq!(266
url_bar_text("https://docs.example/a/b?c=1"),267
"docs.example/a/b?c=1"268
);269
assert_eq!(url_bar_text("http://plain.example"), "http://plain.example");270
}271
}@@ -1,28 +0,0 @@
1
#![allow(dead_code)]
2
3
use gpui_kit::*;
4
use tokio::sync::mpsc::UnboundedReceiver;
5
6
pub enum NativeEvent {
7
Navigated(String),
8
NewWindow(String),
9
Loaded,
10
Closed,
11
}
12
13
pub fn available() -> bool {
14
false
15
}
16
17
#[derive(Clone)]
18
pub struct Native;
19
20
impl Native {
21
pub fn open(_url: &str, _window: &Window) -> Option<(Native, UnboundedReceiver<NativeEvent>)> {
22
None
23
}
24
25
pub fn place(&self, _bounds: Bounds<Pixels>, _scale: f32, _visible: bool) {}
26
27
pub fn close(&self) {}
28
}
@@ -1,28 +0,0 @@1
#![allow(dead_code)]2
3
use gpui_kit::*;4
use tokio::sync::mpsc::UnboundedReceiver;5
6
pub enum NativeEvent {7
Navigated(String),8
NewWindow(String),9
Loaded,10
Closed,11
}12
13
pub fn available() -> bool {14
false15
}16
17
#[derive(Clone)]18
pub struct Native;19
20
impl Native {21
pub fn open(_url: &str, _window: &Window) -> Option<(Native, UnboundedReceiver<NativeEvent>)> {22
None23
}24
25
pub fn place(&self, _bounds: Bounds<Pixels>, _scale: f32, _visible: bool) {}26
27
pub fn close(&self) {}28
}@@ -1,75 +0,0 @@
1
use std::cell::{Cell, OnceCell};
2
use std::path::PathBuf;
3
use std::rc::Rc;
4
use std::sync::OnceLock;
5
6
use gpui_kit::*;
7
use tokio::sync::mpsc::UnboundedReceiver;
8
use webview::{EmbedBounds, EmbedHost, EmbedSpec, MainEmbed};
9
10
use crate::{notify, theme};
11
12
pub use webview::EmbedEvent as NativeEvent;
13
14
static USER_DATA_FOLDER: OnceLock<PathBuf> = OnceLock::new();
15
16
thread_local! {
17
static HOST: OnceCell<EmbedHost> = const { OnceCell::new() };
18
}
19
20
pub fn configure(user_data_folder: PathBuf) {
21
let _ = USER_DATA_FOLDER.set(user_data_folder);
22
}
23
24
pub fn available() -> bool {
25
USER_DATA_FOLDER.get().is_some()
26
}
27
28
fn host() -> Option<EmbedHost> {
29
let folder = USER_DATA_FOLDER.get()?;
30
Some(HOST.with(|host| host.get_or_init(|| EmbedHost::new(folder.clone())).clone()))
31
}
32
33
#[derive(Clone)]
34
pub struct Native {
35
embed: MainEmbed,
36
placed: Rc<Cell<Option<(EmbedBounds, bool)>>>,
37
}
38
39
impl Native {
40
pub fn open(url: &str, window: &Window) -> Option<(Native, UnboundedReceiver<NativeEvent>)> {
41
let parent = notify::native_handle(window)?;
42
let (embed, events) = host()?.open(EmbedSpec {
43
parent,
44
url: url.to_owned(),
45
background: theme::BACKGROUND,
46
})?;
47
let native = Native {
48
embed,
49
placed: Rc::new(Cell::new(None)),
50
};
51
Some((native, events))
52
}
53
54
pub fn place(&self, bounds: Bounds<Pixels>, scale: f32, visible: bool) {
55
let placement = (
56
EmbedBounds::from_logical(
57
f32::from(bounds.origin.x),
58
f32::from(bounds.origin.y),
59
f32::from(bounds.size.width),
60
f32::from(bounds.size.height),
61
scale,
62
),
63
visible,
64
);
65
if self.placed.get() == Some(placement) {
66
return;
67
}
68
self.placed.set(Some(placement));
69
self.embed.place(placement.0, placement.1);
70
}
71
72
pub fn close(&self) {
73
self.embed.close();
74
}
75
}
@@ -1,75 +0,0 @@1
use std::cell::{Cell, OnceCell};2
use std::path::PathBuf;3
use std::rc::Rc;4
use std::sync::OnceLock;5
6
use gpui_kit::*;7
use tokio::sync::mpsc::UnboundedReceiver;8
use webview::{EmbedBounds, EmbedHost, EmbedSpec, MainEmbed};9
10
use crate::{notify, theme};11
12
pub use webview::EmbedEvent as NativeEvent;13
14
static USER_DATA_FOLDER: OnceLock<PathBuf> = OnceLock::new();15
16
thread_local! {17
static HOST: OnceCell<EmbedHost> = const { OnceCell::new() };18
}19
20
pub fn configure(user_data_folder: PathBuf) {21
let _ = USER_DATA_FOLDER.set(user_data_folder);22
}23
24
pub fn available() -> bool {25
USER_DATA_FOLDER.get().is_some()26
}27
28
fn host() -> Option<EmbedHost> {29
let folder = USER_DATA_FOLDER.get()?;30
Some(HOST.with(|host| host.get_or_init(|| EmbedHost::new(folder.clone())).clone()))31
}32
33
#[derive(Clone)]34
pub struct Native {35
embed: MainEmbed,36
placed: Rc<Cell<Option<(EmbedBounds, bool)>>>,37
}38
39
impl Native {40
pub fn open(url: &str, window: &Window) -> Option<(Native, UnboundedReceiver<NativeEvent>)> {41
let parent = notify::native_handle(window)?;42
let (embed, events) = host()?.open(EmbedSpec {43
parent,44
url: url.to_owned(),45
background: theme::BACKGROUND,46
})?;47
let native = Native {48
embed,49
placed: Rc::new(Cell::new(None)),50
};51
Some((native, events))52
}53
54
pub fn place(&self, bounds: Bounds<Pixels>, scale: f32, visible: bool) {55
let placement = (56
EmbedBounds::from_logical(57
f32::from(bounds.origin.x),58
f32::from(bounds.origin.y),59
f32::from(bounds.size.width),60
f32::from(bounds.size.height),61
scale,62
),63
visible,64
);65
if self.placed.get() == Some(placement) {66
return;67
}68
self.placed.set(Some(placement));69
self.embed.place(placement.0, placement.1);70
}71
72
pub fn close(&self) {73
self.embed.close();74
}75
}@@ -27,7 +27,6 @@ mod demo_input_cards;
27
27
mod demo_library;
28
28
mod demo_profiles;
29
29
mod downloads;
30
mod embedded_web;
31
30
mod emoji;
32
31
mod format;
33
32
mod frame_log;
@@ -159,7 +158,6 @@ fn transport(endpoint: Option<&str>, database: Option<&std::path::Path>) -> Arc<
159
158
Some(directory) => directory.join(WEBVIEW_FOLDER),
160
159
None => data_path(WEBVIEW_FOLDER),
161
160
};
162
embedded_web::configure(user_data_folder.clone());
163
161
let transport = webview::start(webview::HostConfig {
164
162
user_data_folder,
165
163
window_title: format!("{APP_NAME} - Sign in"),
@@ -27,7 +27,6 @@ mod demo_input_cards;27
mod demo_library;27
mod demo_library;28
mod demo_profiles;28
mod demo_profiles;29
mod downloads;29
mod downloads;30
mod embedded_web;31
mod emoji;30
mod emoji;32
mod format;31
mod format;33
mod frame_log;32
mod frame_log;@@ -159,7 +158,6 @@ fn transport(endpoint: Option<&str>, database: Option<&std::path::Path>) -> Arc<159
Some(directory) => directory.join(WEBVIEW_FOLDER),158
Some(directory) => directory.join(WEBVIEW_FOLDER),160
None => data_path(WEBVIEW_FOLDER),159
None => data_path(WEBVIEW_FOLDER),161
};160
};162
embedded_web::configure(user_data_folder.clone());163
let transport = webview::start(webview::HostConfig {161
let transport = webview::start(webview::HostConfig {164
user_data_folder,162
user_data_folder,165
window_title: format!("{APP_NAME} - Sign in"),163
window_title: format!("{APP_NAME} - Sign in"),@@ -30,7 +30,6 @@ pub const SHARED_LABEL: &str = "Shared";
30
30
pub enum ChannelPane {
31
31
Posts,
32
32
Shared,
33
Web,
34
33
}
35
34
36
35
pub type SelectPane = Rc<dyn Fn(ChannelPane, &mut App)>;
@@ -44,18 +43,12 @@ pub struct TabBarActions {
44
43
45
44
#[derive(Debug, Clone, PartialEq, Eq)]
46
45
pub enum TabAction {
47
Embed(String),
48
46
Browser(String),
49
47
ResolveLink,
50
48
}
51
49
52
pub fn tab_action(
53
tab: &ChannelTabRecord,
54
embedding: bool,
55
resolved_link: Option<&str>,
56
) -> TabAction {
50
pub fn tab_action(tab: &ChannelTabRecord, resolved_link: Option<&str>) -> TabAction {
57
51
match (&tab.open_url, resolved_link) {
58
(Some(url), _) if embedding && tab.is_website() => TabAction::Embed(url.clone()),
59
52
(Some(url), _) => TabAction::Browser(url.clone()),
60
53
(None, Some(link)) => TabAction::Browser(link.to_owned()),
61
54
(None, None) => TabAction::ResolveLink,
@@ -129,11 +122,7 @@ fn tab_label(
129
122
})
130
123
}
131
124
132
fn overflow_button(
133
hidden: &[(usize, String, bool)],
134
embedding: bool,
135
actions: &TabBarActions,
136
) -> impl IntoElement {
125
fn overflow_button(hidden: &[(usize, String)], actions: &TabBarActions) -> impl IntoElement {
137
126
let entries = hidden.to_vec();
138
127
let open = actions.open.clone();
139
128
Button::new("channel-tab-overflow")
@@ -141,15 +130,10 @@ fn overflow_button(
141
130
.compact()
142
131
.label(format!("+{}", hidden.len()))
143
132
.dropdown_menu(move |menu, _, _| {
144
entries.iter().fold(menu, |menu, (index, name, website)| {
133
entries.iter().fold(menu, |menu, (index, name)| {
145
134
let (open, index) = (open.clone(), *index);
146
let label = if embedding && *website {
147
name.clone()
148
} else {
149
format!("{name} {EXTERNAL_ARROW}")
150
};
151
135
menu.item(
152
PopupMenuItem::new(label)
136
PopupMenuItem::new(format!("{name} {EXTERNAL_ARROW}"))
153
137
.on_click(move |_, window, cx| open(index, window, cx)),
154
138
)
155
139
})
@@ -159,8 +143,6 @@ fn overflow_button(
159
143
pub fn render_tab_bar(
160
144
tabs: &[ChannelTabRecord],
161
145
pane: ChannelPane,
162
active_tab_id: Option<&str>,
163
embedding: bool,
164
146
available_width: f32,
165
147
actions: &TabBarActions,
166
148
) -> AnyElement {
@@ -188,16 +170,16 @@ pub fn render_tab_bar(
188
170
tab_label(
189
171
ElementId::NamedInteger("channel-tab".into(), index as u64),
190
172
&tab.name,
191
active_tab_id == Some(tab.tab_id.as_str()),
192
!(embedding && tab.is_website()),
173
false,
174
true,
193
175
move |window, cx| open(index, window, cx),
194
176
)
195
177
});
196
let hidden: Vec<(usize, String, bool)> = tabs
178
let hidden: Vec<(usize, String)> = tabs
197
179
.iter()
198
180
.enumerate()
199
181
.skip(visible)
200
.map(|(index, tab)| (index, tab.name.clone(), tab.is_website()))
182
.map(|(index, tab)| (index, tab.name.clone()))
201
183
.collect();
202
184
h_flex()
203
185
.w_full()
@@ -210,7 +192,7 @@ pub fn render_tab_bar(
210
192
.child(shared)
211
193
.children(configured)
212
194
.when(!hidden.is_empty(), |bar| {
213
bar.child(overflow_button(&hidden, embedding, actions))
195
bar.child(overflow_button(&hidden, actions))
214
196
})
215
197
.into_any_element()
216
198
}
@@ -238,14 +220,9 @@ mod tests {
238
220
}
239
221
240
222
#[test]
241
fn website_tabs_embed_only_where_the_host_can() {
242
let docs = website("https://docs.example");
243
assert_eq!(
244
tab_action(&docs, true, None),
245
TabAction::Embed("https://docs.example".to_owned())
246
);
223
fn website_tabs_open_in_the_browser() {
247
224
assert_eq!(
248
tab_action(&docs, false, None),
225
tab_action(&website("https://docs.example"), None),
249
226
TabAction::Browser("https://docs.example".to_owned())
250
227
);
251
228
}
@@ -254,7 +231,7 @@ mod tests {
254
231
fn other_tabs_with_a_page_open_it_in_the_browser() {
255
232
let notes = tab("Notes", Some("https://onenote.example/web"));
256
233
assert_eq!(
257
tab_action(¬es, true, None),
234
tab_action(¬es, None),
258
235
TabAction::Browser("https://onenote.example/web".to_owned())
259
236
);
260
237
}
@@ -262,9 +239,9 @@ mod tests {
262
239
#[test]
263
240
fn app_tabs_resolve_their_link_once_and_then_reuse_it() {
264
241
let board = tab("Board", None);
265
assert_eq!(tab_action(&board, true, None), TabAction::ResolveLink);
242
assert_eq!(tab_action(&board, None), TabAction::ResolveLink);
266
243
assert_eq!(
267
tab_action(&board, true, Some("https://teams.example/l/tab")),
244
tab_action(&board, Some("https://teams.example/l/tab")),
268
245
TabAction::Browser("https://teams.example/l/tab".to_owned())
269
246
);
270
247
}
@@ -30,7 +30,6 @@ pub const SHARED_LABEL: &str = "Shared";30
pub enum ChannelPane {30
pub enum ChannelPane {31
Posts,31
Posts,32
Shared,32
Shared,33
Web,34
}33
}35
34
36
pub type SelectPane = Rc<dyn Fn(ChannelPane, &mut App)>;35
pub type SelectPane = Rc<dyn Fn(ChannelPane, &mut App)>;@@ -44,18 +43,12 @@ pub struct TabBarActions {44
43
45
#[derive(Debug, Clone, PartialEq, Eq)]44
#[derive(Debug, Clone, PartialEq, Eq)]46
pub enum TabAction {45
pub enum TabAction {47
Embed(String),48
Browser(String),46
Browser(String),49
ResolveLink,47
ResolveLink,50
}48
}51
49
52
pub fn tab_action(50
pub fn tab_action(tab: &ChannelTabRecord, resolved_link: Option<&str>) -> TabAction {53
tab: &ChannelTabRecord,54
embedding: bool,55
resolved_link: Option<&str>,56
) -> TabAction {57
match (&tab.open_url, resolved_link) {51
match (&tab.open_url, resolved_link) {58
(Some(url), _) if embedding && tab.is_website() => TabAction::Embed(url.clone()),59
(Some(url), _) => TabAction::Browser(url.clone()),52
(Some(url), _) => TabAction::Browser(url.clone()),60
(None, Some(link)) => TabAction::Browser(link.to_owned()),53
(None, Some(link)) => TabAction::Browser(link.to_owned()),61
(None, None) => TabAction::ResolveLink,54
(None, None) => TabAction::ResolveLink,@@ -129,11 +122,7 @@ fn tab_label(129
})122
})130
}123
}131
124
132
fn overflow_button(125
fn overflow_button(hidden: &[(usize, String)], actions: &TabBarActions) -> impl IntoElement {133
hidden: &[(usize, String, bool)],134
embedding: bool,135
actions: &TabBarActions,136
) -> impl IntoElement {137
let entries = hidden.to_vec();126
let entries = hidden.to_vec();138
let open = actions.open.clone();127
let open = actions.open.clone();139
Button::new("channel-tab-overflow")128
Button::new("channel-tab-overflow")@@ -141,15 +130,10 @@ fn overflow_button(141
.compact()130
.compact()142
.label(format!("+{}", hidden.len()))131
.label(format!("+{}", hidden.len()))143
.dropdown_menu(move |menu, _, _| {132
.dropdown_menu(move |menu, _, _| {144
entries.iter().fold(menu, |menu, (index, name, website)| {133
entries.iter().fold(menu, |menu, (index, name)| {145
let (open, index) = (open.clone(), *index);134
let (open, index) = (open.clone(), *index);146
let label = if embedding && *website {147
name.clone()148
} else {149
format!("{name} {EXTERNAL_ARROW}")150
};151
menu.item(135
menu.item(152
PopupMenuItem::new(label)136
PopupMenuItem::new(format!("{name} {EXTERNAL_ARROW}"))153
.on_click(move |_, window, cx| open(index, window, cx)),137
.on_click(move |_, window, cx| open(index, window, cx)),154
)138
)155
})139
})@@ -159,8 +143,6 @@ fn overflow_button(159
pub fn render_tab_bar(143
pub fn render_tab_bar(160
tabs: &[ChannelTabRecord],144
tabs: &[ChannelTabRecord],161
pane: ChannelPane,145
pane: ChannelPane,162
active_tab_id: Option<&str>,163
embedding: bool,164
available_width: f32,146
available_width: f32,165
actions: &TabBarActions,147
actions: &TabBarActions,166
) -> AnyElement {148
) -> AnyElement {@@ -188,16 +170,16 @@ pub fn render_tab_bar(188
tab_label(170
tab_label(189
ElementId::NamedInteger("channel-tab".into(), index as u64),171
ElementId::NamedInteger("channel-tab".into(), index as u64),190
&tab.name,172
&tab.name,191
active_tab_id == Some(tab.tab_id.as_str()),173
false,192
!(embedding && tab.is_website()),174
true,193
move |window, cx| open(index, window, cx),175
move |window, cx| open(index, window, cx),194
)176
)195
});177
});196
let hidden: Vec<(usize, String, bool)> = tabs178
let hidden: Vec<(usize, String)> = tabs197
.iter()179
.iter()198
.enumerate()180
.enumerate()199
.skip(visible)181
.skip(visible)200
.map(|(index, tab)| (index, tab.name.clone(), tab.is_website()))182
.map(|(index, tab)| (index, tab.name.clone()))201
.collect();183
.collect();202
h_flex()184
h_flex()203
.w_full()185
.w_full()@@ -210,7 +192,7 @@ pub fn render_tab_bar(210
.child(shared)192
.child(shared)211
.children(configured)193
.children(configured)212
.when(!hidden.is_empty(), |bar| {194
.when(!hidden.is_empty(), |bar| {213
bar.child(overflow_button(&hidden, embedding, actions))195
bar.child(overflow_button(&hidden, actions))214
})196
})215
.into_any_element()197
.into_any_element()216
}198
}@@ -238,14 +220,9 @@ mod tests {238
}220
}239
221
240
#[test]222
#[test]241
fn website_tabs_embed_only_where_the_host_can() {223
fn website_tabs_open_in_the_browser() {242
let docs = website("https://docs.example");243
assert_eq!(244
tab_action(&docs, true, None),245
TabAction::Embed("https://docs.example".to_owned())246
);247
assert_eq!(224
assert_eq!(248
tab_action(&docs, false, None),225
tab_action(&website("https://docs.example"), None),249
TabAction::Browser("https://docs.example".to_owned())226
TabAction::Browser("https://docs.example".to_owned())250
);227
);251
}228
}@@ -254,7 +231,7 @@ mod tests {254
fn other_tabs_with_a_page_open_it_in_the_browser() {231
fn other_tabs_with_a_page_open_it_in_the_browser() {255
let notes = tab("Notes", Some("https://onenote.example/web"));232
let notes = tab("Notes", Some("https://onenote.example/web"));256
assert_eq!(233
assert_eq!(257
tab_action(¬es, true, None),234
tab_action(¬es, None),258
TabAction::Browser("https://onenote.example/web".to_owned())235
TabAction::Browser("https://onenote.example/web".to_owned())259
);236
);260
}237
}@@ -262,9 +239,9 @@ mod tests {262
#[test]239
#[test]263
fn app_tabs_resolve_their_link_once_and_then_reuse_it() {240
fn app_tabs_resolve_their_link_once_and_then_reuse_it() {264
let board = tab("Board", None);241
let board = tab("Board", None);265
assert_eq!(tab_action(&board, true, None), TabAction::ResolveLink);242
assert_eq!(tab_action(&board, None), TabAction::ResolveLink);266
assert_eq!(243
assert_eq!(267
tab_action(&board, true, Some("https://teams.example/l/tab")),244
tab_action(&board, Some("https://teams.example/l/tab")),268
TabAction::Browser("https://teams.example/l/tab".to_owned())245
TabAction::Browser("https://teams.example/l/tab".to_owned())269
);246
);270
}247
}@@ -47,7 +47,6 @@ use crate::channel_files::DownloadSource;
47
47
use crate::data::{is_one_on_one, others};
48
48
use crate::demo_library::DemoLibrary;
49
49
use crate::downloads::{self, ClickAction, DownloadKey, Downloads, PartFile, RevealTarget};
50
use crate::embedded_web::EmbeddedWeb;
51
50
use crate::emoji;
52
51
use crate::image_view_gallery::{ViewerRequest, collect_gallery, position_of};
53
52
use crate::notice::short_error;
@@ -186,9 +185,7 @@ pub struct ConversationView {
186
185
pane: ChannelPane,
187
186
channel_tabs: Vec<ChannelTabRecord>,
188
187
tab_links: HashMap<String, String>,
189
web: Option<Entity<EmbeddedWeb>>,
190
188
focus_handle: FocusHandle,
191
web_subscription: Option<Subscription>,
192
189
shared: SharedState,
193
190
demo_library: DemoLibrary,
194
191
new_folder_input: Option<Entity<InputState>>,
@@ -487,9 +484,7 @@ impl ConversationView {
487
484
pane: ChannelPane::Posts,
488
485
channel_tabs: Vec::new(),
489
486
tab_links: HashMap::new(),
490
web: None,
491
487
focus_handle: cx.focus_handle(),
492
web_subscription: None,
493
488
shared: SharedState::default(),
494
489
demo_library: DemoLibrary::default(),
495
490
new_folder_input: None,
@@ -1029,7 +1024,6 @@ impl ConversationView {
1029
1024
let in_channel = matches!(current.selection, Selection::Channel(_));
1030
1025
if in_channel {
1031
1026
self.pane = ChannelPane::Posts;
1032
self.close_web();
1033
1027
match record.reply_to_id.clone() {
1034
1028
Some(root_id) => {
1035
1029
self.enter_thread_mode(root_id.clone(), cx);
@@ -3075,9 +3069,6 @@ impl ConversationView {
3075
3069
3076
3070
impl Render for ConversationView {
3077
3071
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
3078
if self.web.is_some() && !self.web_active() {
3079
self.close_web();
3080
}
3081
3072
let background = theme::surface();
3082
3073
let mut root = v_flex()
3083
3074
.flex_1()
@@ -3222,8 +3213,6 @@ impl Render for ConversationView {
3222
3213
draft.render_empty(&self.app.read(cx).directory, &group_name)
3223
3214
} else if self.shared_active() {
3224
3215
self.render_shared(cx)
3225
} else if let Some(web) = self.web.clone().filter(|_| self.web_active()) {
3226
web.into_any_element()
3227
3216
} else if empty_channel {
3228
3217
div()
3229
3218
.size_full()
@@ -47,7 +47,6 @@ use crate::channel_files::DownloadSource;47
use crate::data::{is_one_on_one, others};47
use crate::data::{is_one_on_one, others};48
use crate::demo_library::DemoLibrary;48
use crate::demo_library::DemoLibrary;49
use crate::downloads::{self, ClickAction, DownloadKey, Downloads, PartFile, RevealTarget};49
use crate::downloads::{self, ClickAction, DownloadKey, Downloads, PartFile, RevealTarget};50
use crate::embedded_web::EmbeddedWeb;51
use crate::emoji;50
use crate::emoji;52
use crate::image_view_gallery::{ViewerRequest, collect_gallery, position_of};51
use crate::image_view_gallery::{ViewerRequest, collect_gallery, position_of};53
use crate::notice::short_error;52
use crate::notice::short_error;@@ -186,9 +185,7 @@ pub struct ConversationView {186
pane: ChannelPane,185
pane: ChannelPane,187
channel_tabs: Vec<ChannelTabRecord>,186
channel_tabs: Vec<ChannelTabRecord>,188
tab_links: HashMap<String, String>,187
tab_links: HashMap<String, String>,189
web: Option<Entity<EmbeddedWeb>>,190
focus_handle: FocusHandle,188
focus_handle: FocusHandle,191
web_subscription: Option<Subscription>,192
shared: SharedState,189
shared: SharedState,193
demo_library: DemoLibrary,190
demo_library: DemoLibrary,194
new_folder_input: Option<Entity<InputState>>,191
new_folder_input: Option<Entity<InputState>>,@@ -487,9 +484,7 @@ impl ConversationView {487
pane: ChannelPane::Posts,484
pane: ChannelPane::Posts,488
channel_tabs: Vec::new(),485
channel_tabs: Vec::new(),489
tab_links: HashMap::new(),486
tab_links: HashMap::new(),490
web: None,491
focus_handle: cx.focus_handle(),487
focus_handle: cx.focus_handle(),492
web_subscription: None,493
shared: SharedState::default(),488
shared: SharedState::default(),494
demo_library: DemoLibrary::default(),489
demo_library: DemoLibrary::default(),495
new_folder_input: None,490
new_folder_input: None,@@ -1029,7 +1024,6 @@ impl ConversationView {1029
let in_channel = matches!(current.selection, Selection::Channel(_));1024
let in_channel = matches!(current.selection, Selection::Channel(_));1030
if in_channel {1025
if in_channel {1031
self.pane = ChannelPane::Posts;1026
self.pane = ChannelPane::Posts;1032
self.close_web();1033
match record.reply_to_id.clone() {1027
match record.reply_to_id.clone() {1034
Some(root_id) => {1028
Some(root_id) => {1035
self.enter_thread_mode(root_id.clone(), cx);1029
self.enter_thread_mode(root_id.clone(), cx);@@ -3075,9 +3069,6 @@ impl ConversationView {3075
3069
3076
impl Render for ConversationView {3070
impl Render for ConversationView {3077
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {3071
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {3078
if self.web.is_some() && !self.web_active() {3079
self.close_web();3080
}3081
let background = theme::surface();3072
let background = theme::surface();3082
let mut root = v_flex()3073
let mut root = v_flex()3083
.flex_1()3074
.flex_1()@@ -3222,8 +3213,6 @@ impl Render for ConversationView {3222
draft.render_empty(&self.app.read(cx).directory, &group_name)3213
draft.render_empty(&self.app.read(cx).directory, &group_name)3223
} else if self.shared_active() {3214
} else if self.shared_active() {3224
self.render_shared(cx)3215
self.render_shared(cx)3225
} else if let Some(web) = self.web.clone().filter(|_| self.web_active()) {3226
web.into_any_element()3227
} else if empty_channel {3216
} else if empty_channel {3228
div()3217
div()3229
.size_full()3218
.size_full()@@ -29,7 +29,6 @@ use super::title_bar::render_title_bar;
29
29
use crate::activity::ActivityCenter;
30
30
use crate::app_state::{AppEvent, AppState, Selection};
31
31
use crate::call::{CallMedia, CallWindows};
32
use crate::embedded_web::{Overlays, RootFocus};
33
32
use crate::notice::NoticeAction;
34
33
use crate::user_activity::{self, UserInput};
35
34
use crate::notify::{NotificationCenter, selection_for};
@@ -161,7 +160,6 @@ impl AppShell {
161
160
});
162
161
let focus_handle = cx.focus_handle();
163
162
window.focus(&focus_handle, cx);
164
cx.set_global(RootFocus(focus_handle.clone()));
165
163
let notifications = cx.new(|cx| NotificationCenter::new(state.clone(), window, cx));
166
164
let call_windows = cx.new(|cx| CallWindows::new(state.clone(), window, cx));
167
165
let user_input = cx.global::<UserInputHandle>().0.clone();
@@ -591,17 +589,6 @@ impl Render for AppShell {
591
589
});
592
590
});
593
591
}
594
let panels = self.switcher.is_some()
595
|| self.forward_dialog.is_some()
596
|| self.image_viewer.is_some()
597
|| self.status_dialog.is_some()
598
|| self.shortcuts_dialog.is_some()
599
|| self.profile_card.is_some()
600
|| {
601
let state = self.state.read(cx);
602
state.task_dialog.is_some() || state.status_menu_open
603
};
604
Overlays::update(cx, |overlays| overlays.panels = panels);
605
592
let chat_thread = self.state.read(cx).call_chat_thread();
606
593
self.sync_call_chat(chat_thread, window, cx);
607
594
let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone());
@@ -29,7 +29,6 @@ use super::title_bar::render_title_bar;29
use crate::activity::ActivityCenter;29
use crate::activity::ActivityCenter;30
use crate::app_state::{AppEvent, AppState, Selection};30
use crate::app_state::{AppEvent, AppState, Selection};31
use crate::call::{CallMedia, CallWindows};31
use crate::call::{CallMedia, CallWindows};32
use crate::embedded_web::{Overlays, RootFocus};33
use crate::notice::NoticeAction;32
use crate::notice::NoticeAction;34
use crate::user_activity::{self, UserInput};33
use crate::user_activity::{self, UserInput};35
use crate::notify::{NotificationCenter, selection_for};34
use crate::notify::{NotificationCenter, selection_for};@@ -161,7 +160,6 @@ impl AppShell {161
});160
});162
let focus_handle = cx.focus_handle();161
let focus_handle = cx.focus_handle();163
window.focus(&focus_handle, cx);162
window.focus(&focus_handle, cx);164
cx.set_global(RootFocus(focus_handle.clone()));165
let notifications = cx.new(|cx| NotificationCenter::new(state.clone(), window, cx));163
let notifications = cx.new(|cx| NotificationCenter::new(state.clone(), window, cx));166
let call_windows = cx.new(|cx| CallWindows::new(state.clone(), window, cx));164
let call_windows = cx.new(|cx| CallWindows::new(state.clone(), window, cx));167
let user_input = cx.global::<UserInputHandle>().0.clone();165
let user_input = cx.global::<UserInputHandle>().0.clone();@@ -591,17 +589,6 @@ impl Render for AppShell {591
});589
});592
});590
});593
}591
}594
let panels = self.switcher.is_some()595
|| self.forward_dialog.is_some()596
|| self.image_viewer.is_some()597
|| self.status_dialog.is_some()598
|| self.shortcuts_dialog.is_some()599
|| self.profile_card.is_some()600
|| {601
let state = self.state.read(cx);602
state.task_dialog.is_some() || state.status_menu_open603
};604
Overlays::update(cx, |overlays| overlays.panels = panels);605
let chat_thread = self.state.read(cx).call_chat_thread();592
let chat_thread = self.state.read(cx).call_chat_thread();606
self.sync_call_chat(chat_thread, window, cx);593
self.sync_call_chat(chat_thread, window, cx);607
let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone());594
let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone());@@ -1,3 +1,4 @@
1
use percent_encoding::percent_decode_str;
1
2
use serde_json::Value;
2
3
use session::Request;
3
4
@@ -68,6 +69,12 @@ fn tab_open_url(settings: &Value) -> Option<String> {
68
69
.map(str::to_owned)
69
70
}
70
71
72
fn decode_tab_name(name: &str) -> String {
73
percent_decode_str(name)
74
.decode_utf8()
75
.map_or_else(|_| name.to_owned(), |decoded| decoded.into_owned())
76
}
77
71
78
fn parse_tab(tab: &Value) -> Option<(i64, ChannelTab)> {
72
79
let definition_id = tab.get("definitionId")?.as_str()?;
73
80
if definition_id == WIKI_DEFINITION_ID {
@@ -82,7 +89,7 @@ fn parse_tab(tab: &Value) -> Option<(i64, ChannelTab)> {
82
89
order,
83
90
ChannelTab {
84
91
id: tab.get("id")?.as_str()?.to_owned(),
85
name: tab.get("name")?.as_str()?.to_owned(),
92
name: decode_tab_name(tab.get("name")?.as_str()?),
86
93
definition_id: definition_id.to_owned(),
87
94
open_url: tab_open_url(settings),
88
95
},
@@ -259,6 +266,24 @@ mod tests {
259
266
);
260
267
}
261
268
269
#[test]
270
fn percent_encoded_tab_names_are_decoded() {
271
let channel = json!({"id": "19:a@thread.tacv2", "tabs": [
272
{"id": "t1", "name": "Lunch%20Order%20Board%20%F0%9F%8D%BD%EF%B8%8F",
273
"definitionId": "com.microsoft.teamspace.tab.web", "order": 1},
274
{"id": "t2", "name": "100% done", "definitionId": "com.microsoft.teamspace.tab.web", "order": 2},
275
{"id": "t3", "name": "Bad%FFbytes", "definitionId": "com.microsoft.teamspace.tab.web", "order": 3}
276
]});
277
let names: Vec<String> = parse_channel_tabs(&channel)
278
.into_iter()
279
.map(|tab| tab.name)
280
.collect();
281
assert_eq!(
282
names,
283
["Lunch Order Board \u{1F37D}\u{FE0F}", "100% done", "Bad%FFbytes"]
284
);
285
}
286
262
287
#[test]
263
288
fn a_channel_without_tabs_has_none() {
264
289
assert!(parse_channel_tabs(&json!({"id": "19:a@thread.tacv2"})).is_empty());
@@ -1,3 +1,4 @@1
use percent_encoding::percent_decode_str;1
use serde_json::Value;2
use serde_json::Value;2
use session::Request;3
use session::Request;3
4
@@ -68,6 +69,12 @@ fn tab_open_url(settings: &Value) -> Option<String> {68
.map(str::to_owned)69
.map(str::to_owned)69
}70
}70
71
72
fn decode_tab_name(name: &str) -> String {73
percent_decode_str(name)74
.decode_utf8()75
.map_or_else(|_| name.to_owned(), |decoded| decoded.into_owned())76
}77
71
fn parse_tab(tab: &Value) -> Option<(i64, ChannelTab)> {78
fn parse_tab(tab: &Value) -> Option<(i64, ChannelTab)> {72
let definition_id = tab.get("definitionId")?.as_str()?;79
let definition_id = tab.get("definitionId")?.as_str()?;73
if definition_id == WIKI_DEFINITION_ID {80
if definition_id == WIKI_DEFINITION_ID {@@ -82,7 +89,7 @@ fn parse_tab(tab: &Value) -> Option<(i64, ChannelTab)> {82
order,89
order,83
ChannelTab {90
ChannelTab {84
id: tab.get("id")?.as_str()?.to_owned(),91
id: tab.get("id")?.as_str()?.to_owned(),85
name: tab.get("name")?.as_str()?.to_owned(),92
name: decode_tab_name(tab.get("name")?.as_str()?),86
definition_id: definition_id.to_owned(),93
definition_id: definition_id.to_owned(),87
open_url: tab_open_url(settings),94
open_url: tab_open_url(settings),88
},95
},@@ -259,6 +266,24 @@ mod tests {259
);266
);260
}267
}261
268
269
#[test]270
fn percent_encoded_tab_names_are_decoded() {271
let channel = json!({"id": "19:a@thread.tacv2", "tabs": [272
{"id": "t1", "name": "Lunch%20Order%20Board%20%F0%9F%8D%BD%EF%B8%8F",273
"definitionId": "com.microsoft.teamspace.tab.web", "order": 1},274
{"id": "t2", "name": "100% done", "definitionId": "com.microsoft.teamspace.tab.web", "order": 2},275
{"id": "t3", "name": "Bad%FFbytes", "definitionId": "com.microsoft.teamspace.tab.web", "order": 3}276
]});277
let names: Vec<String> = parse_channel_tabs(&channel)278
.into_iter()279
.map(|tab| tab.name)280
.collect();281
assert_eq!(282
names,283
["Lunch Order Board \u{1F37D}\u{FE0F}", "100% done", "Bad%FFbytes"]284
);285
}286
262
#[test]287
#[test]263
fn a_channel_without_tabs_has_none() {288
fn a_channel_without_tabs_has_none() {264
assert!(parse_channel_tabs(&json!({"id": "19:a@thread.tacv2"})).is_empty());289
assert!(parse_channel_tabs(&json!({"id": "19:a@thread.tacv2"})).is_empty());@@ -169,14 +169,6 @@ pub struct ChannelTabRecord {
169
169
pub open_url: Option<String>,
170
170
}
171
171
172
const WEBSITE_DEFINITION_ID: &str = "com.microsoft.teamspace.tab.web";
173
174
impl ChannelTabRecord {
175
pub fn is_website(&self) -> bool {
176
self.definition_id == WEBSITE_DEFINITION_ID && self.open_url.is_some()
177
}
178
}
179
180
172
#[derive(Debug, Clone, PartialEq, Eq)]
181
173
pub struct ChannelLayoutRecord {
182
174
pub channel_id: String,
@@ -169,14 +169,6 @@ pub struct ChannelTabRecord {169
pub open_url: Option<String>,169
pub open_url: Option<String>,170
}170
}171
171
172
const WEBSITE_DEFINITION_ID: &str = "com.microsoft.teamspace.tab.web";173
174
impl ChannelTabRecord {175
pub fn is_website(&self) -> bool {176
self.definition_id == WEBSITE_DEFINITION_ID && self.open_url.is_some()177
}178
}179
180
#[derive(Debug, Clone, PartialEq, Eq)]172
#[derive(Debug, Clone, PartialEq, Eq)]181
pub struct ChannelLayoutRecord {173
pub struct ChannelLayoutRecord {182
pub channel_id: String,174
pub channel_id: String,