yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use super::style::{radius, size, space, text}; |
| 2 | use crate::lang::w; |
| 3 | use common::call::ForwardMessages; |
| 4 | use common::model::Id; |
| 5 | use gpui_kit::{ |
| 6 | AppContext, Context, Entity, InteractiveElement, IntoElement, ParentElement, Render, |
| 7 | StatefulInteractiveElement, Styled, Window, |
| 8 | component::{ |
| 9 | WindowExt, |
| 10 | input::{InputEvent, InputState}, |
| 11 | }, |
| 12 | div, px, |
| 13 | }; |
| 14 | |
| 15 | use super::{ |
| 16 | Yumush, clickable, community_name, |
| 17 | composer::Composer, |
| 18 | palette, |
| 19 | widget::{compact_field, search_text, spawn_done}, |
| 20 | }; |
| 21 | |
| 22 | pub struct Forward { |
| 23 | yumush: Entity<Yumush>, |
| 24 | selected: Vec<Id>, |
| 25 | filter: Entity<InputState>, |
| 26 | keep_authors: bool, |
| 27 | } |
| 28 | |
| 29 | impl Forward { |
| 30 | fn new( |
| 31 | yumush: Entity<Yumush>, |
| 32 | selected: Vec<Id>, |
| 33 | window: &mut Window, |
| 34 | cx: &mut Context<Self>, |
| 35 | ) -> Self { |
| 36 | let filter = cx.new(|cx| { |
| 37 | let mut state = InputState::new(window, cx); |
| 38 | state.set_placeholder(w().forward.filter, window, cx); |
| 39 | state |
| 40 | }); |
| 41 | cx.subscribe_in(&filter, window, |_, _, event, _, cx| { |
| 42 | if matches!(event, InputEvent::Change) { |
| 43 | cx.notify(); |
| 44 | } |
| 45 | }) |
| 46 | .detach(); |
| 47 | |
| 48 | let keep_authors = selected.len() > 1; |
| 49 | Self { |
| 50 | yumush, |
| 51 | selected, |
| 52 | filter, |
| 53 | keep_authors, |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl Render for Forward { |
| 59 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 60 | let palette = palette(cx); |
| 61 | let query = search_text(&self.filter, cx); |
| 62 | let entity = cx.entity(); |
| 63 | let keep_authors = self.keep_authors; |
| 64 | let rows: Vec<_> = { |
| 65 | let yumush = self.yumush.read(cx); |
| 66 | yumush |
| 67 | .chat |
| 68 | .communities() |
| 69 | .iter() |
| 70 | .filter(|community| community_name(community).to_lowercase().contains(&query)) |
| 71 | .enumerate() |
| 72 | .map(|(index, community)| { |
| 73 | let community_id = community.id; |
| 74 | let selected = self.selected.clone(); |
| 75 | let forward_entity = self.yumush.clone(); |
| 76 | |
| 77 | clickable(("forward_to", index), palette, move |window, cx| { |
| 78 | let selected = selected.clone(); |
| 79 | forward_entity.update(cx, |yumush, cx| { |
| 80 | yumush.forward_to(selected, community_id, keep_authors, window, cx) |
| 81 | }); |
| 82 | }) |
| 83 | .h(px(size::CONTROL_SM)) |
| 84 | .px(px(space::SM)) |
| 85 | .flex() |
| 86 | .items_center() |
| 87 | .gap(px(space::SM)) |
| 88 | .rounded(px(radius::MD)) |
| 89 | .child(yumush.community_badge( |
| 90 | community, |
| 91 | px(size::ICON), |
| 92 | super::text(text::CAPTION), |
| 93 | palette, |
| 94 | )) |
| 95 | .child( |
| 96 | div() |
| 97 | .flex_1() |
| 98 | .min_w_0() |
| 99 | .truncate() |
| 100 | .child(community_name(community).to_string()), |
| 101 | ) |
| 102 | }) |
| 103 | .collect() |
| 104 | }; |
| 105 | |
| 106 | div() |
| 107 | .flex() |
| 108 | .flex_col() |
| 109 | .gap(px(space::MD)) |
| 110 | .child(compact_field(palette, &self.filter)) |
| 111 | .child(super::parts::labeled_toggle( |
| 112 | "keep_authors", |
| 113 | w().forward.keep_authors, |
| 114 | keep_authors, |
| 115 | palette.tokens, |
| 116 | move |_, cx| { |
| 117 | entity.update(cx, |this, cx| { |
| 118 | this.keep_authors = !this.keep_authors; |
| 119 | cx.notify(); |
| 120 | }); |
| 121 | }, |
| 122 | )) |
| 123 | .child( |
| 124 | div() |
| 125 | .id("forward_list") |
| 126 | .max_h(px(size::POPOVER)) |
| 127 | .flex() |
| 128 | .flex_col() |
| 129 | .gap(px(space::XXS)) |
| 130 | .overflow_y_scroll() |
| 131 | .children(rows), |
| 132 | ) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | impl Yumush { |
| 137 | pub(super) fn open_forward( |
| 138 | &mut self, |
| 139 | selected: Vec<Id>, |
| 140 | window: &mut Window, |
| 141 | cx: &mut Context<Self>, |
| 142 | ) { |
| 143 | if selected.is_empty() { |
| 144 | return; |
| 145 | } |
| 146 | let yumush = cx.entity(); |
| 147 | let forward = cx.new(|cx| Forward::new(yumush, selected, window, cx)); |
| 148 | |
| 149 | window.open_dialog(cx, move |dialog, _, _| { |
| 150 | dialog |
| 151 | .title(w().forward.forward) |
| 152 | .w(px(size::SHEET)) |
| 153 | .child(forward.clone()) |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | pub(super) fn forward_to( |
| 158 | &mut self, |
| 159 | selected: Vec<Id>, |
| 160 | community_id: Id, |
| 161 | keep_authors: bool, |
| 162 | window: &mut Window, |
| 163 | cx: &mut Context<Self>, |
| 164 | ) { |
| 165 | window.close_dialog(cx); |
| 166 | if matches!(self.chat.composer, Composer::Select { .. }) { |
| 167 | self.chat.composer = Composer::Compose { reply: None }; |
| 168 | } |
| 169 | |
| 170 | let ids: Vec<Id> = self |
| 171 | .chat |
| 172 | .messages |
| 173 | .iter() |
| 174 | .filter(|message| selected.contains(&message.id)) |
| 175 | .map(|message| message.id) |
| 176 | .collect(); |
| 177 | let network = self.network.clone(); |
| 178 | |
| 179 | spawn_done(window, cx, async move { |
| 180 | network |
| 181 | .call(ForwardMessages { |
| 182 | message_ids: ids, |
| 183 | community_id, |
| 184 | snippet: keep_authors, |
| 185 | }) |
| 186 | .await |
| 187 | }); |
| 188 | cx.notify(); |
| 189 | } |
| 190 | } |