Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: let users change default text editor #959

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions cosmic-settings/src/pages/system/default_apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use cosmic::{
Apply, Element, Task,
};
use cosmic_settings_page::{self as page, section, Section};
use freedesktop_desktop_entry::{default_paths, DesktopEntry, Iter as DesktopEntryIter};
use mime_apps::App;
use slotmap::SlotMap;
use tokio::sync::mpsc;
use freedesktop_desktop_entry::{default_paths, DesktopEntry, Iter as DesktopEntryIter};

const DROPDOWN_WEB_BROWSER: usize = 0;
const DROPDOWN_FILE_MANAGER: usize = 1;
Expand All @@ -26,6 +26,7 @@ const DROPDOWN_VIDEO: usize = 4;
const DROPDOWN_PHOTO: usize = 5;
const DROPDOWN_CALENDAR: usize = 6;
const DROPDOWN_TERMINAL: usize = 7;
const DROPDOWN_TEXT_EDITOR: usize = 8;

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum Category {
Expand All @@ -38,6 +39,7 @@ pub enum Category {
Terminal,
Video,
WebBrowser,
TextEditor,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -128,6 +130,7 @@ impl page::Page<crate::pages::Message> for Page {
load_defaults(&assocs, &["image/png"]).await,
load_defaults(&assocs, &["text/calendar"]).await,
load_terminal_apps(&assocs).await,
load_defaults(&assocs, &["text/plain"]).await,
];

Message::Update(CachedMimeApps {
Expand Down Expand Up @@ -201,10 +204,13 @@ impl Page {
&mime_types
}),
Category::Mail => (DROPDOWN_MAIL, &["x-scheme-handler/mailto"]),
Category::Terminal => (DROPDOWN_TERMINAL, &[
"x-scheme-handler/terminal",
"application/x-terminal-emulator"
]),
Category::Terminal => (
DROPDOWN_TERMINAL,
&[
"x-scheme-handler/terminal",
"application/x-terminal-emulator",
],
),
Category::Video => (DROPDOWN_VIDEO, {
mime_types = mime_apps
.known_mimes
Expand All @@ -224,6 +230,7 @@ impl Page {
"x-scheme-handler/https",
],
),
Category::TextEditor => (DROPDOWN_TEXT_EDITOR, &["text/plain"]),
Category::Mime(_mime_type) => return Task::none(),
};

Expand Down Expand Up @@ -346,6 +353,16 @@ fn apps() -> Section<crate::pages::Message> {
.icons(&meta.icons),
)
})
.add({
let meta = &mime_apps.apps[DROPDOWN_TEXT_EDITOR];
settings::flex_item(
fl!("default-apps", "text-editor"),
dropdown(&meta.apps, meta.selected, |id| {
Message::SetDefault(Category::TextEditor, id)
})
.icons(&meta.icons),
)
})
.apply(Element::from)
.map(crate::pages::Message::DefaultApps)
})
Expand Down Expand Up @@ -454,7 +471,11 @@ async fn load_terminal_apps(assocs: &BTreeMap<Arc<str>, Arc<App>>) -> AppMeta {
if let Ok(bytes) = std::fs::read_to_string(&path) {
if let Ok(entry) = DesktopEntry::from_str(&path, &bytes, None::<&[&str]>) {
// Check if it's a terminal application
if entry.categories().map(|cats| cats.iter().any(|c| *c == "TerminalEmulator")).unwrap_or(false) {
if entry
.categories()
.map(|cats| cats.iter().any(|c| *c == "TerminalEmulator"))
.unwrap_or(false)
{
let id = entry.id();
if let Some(app) = assocs.get(id) {
if current_appid.as_ref().map(|c| *c == id).unwrap_or(false) {
Expand Down
1 change: 1 addition & 0 deletions i18n/en/cosmic_settings.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,4 @@ default-apps = Default Applications
.calendar = Calendar
.terminal = Terminal
.other-associations = Other Associations
.text-editor = Text Editor