-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
3,775 additions
and
508 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fallback_language = "en" | ||
|
||
[fluent] | ||
assets_dir = "./i18n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
allow = Allow | ||
cancel = Cancel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use cosmic::{app, iced::window}; | ||
|
||
use crate::{access, screenshot, subscription}; | ||
|
||
pub(crate) fn run() -> cosmic::iced::Result { | ||
let settings = cosmic::app::Settings::default().no_main_window(true); | ||
cosmic::app::run::<CosmicPortal>(settings, ()) | ||
} | ||
|
||
#[derive(Default, Clone)] | ||
// run iced app with no main surface | ||
pub struct CosmicPortal { | ||
pub core: app::Core, | ||
pub access_args: Option<access::AccessDialogArgs>, | ||
pub access_choices: Vec<(Option<usize>, Vec<String>)>, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Msg { | ||
Access(access::Msg), | ||
Portal(subscription::Event), | ||
} | ||
|
||
impl cosmic::Application for CosmicPortal { | ||
type Executor = cosmic::executor::Default; | ||
|
||
type Flags = (); | ||
|
||
type Message = Msg; | ||
|
||
const APP_ID: &'static str = "org.freedesktop.portal.desktop.cosmic"; | ||
|
||
fn core(&self) -> &app::Core { | ||
&self.core | ||
} | ||
|
||
fn core_mut(&mut self) -> &mut app::Core { | ||
&mut self.core | ||
} | ||
|
||
fn init( | ||
core: app::Core, | ||
_flags: Self::Flags, | ||
) -> (Self, cosmic::iced::Command<app::Message<Self::Message>>) { | ||
( | ||
Self { | ||
core, | ||
..Default::default() | ||
}, | ||
cosmic::iced::Command::none(), | ||
) | ||
} | ||
|
||
fn view(&self) -> cosmic::prelude::Element<Self::Message> { | ||
unimplemented!() | ||
} | ||
|
||
fn view_window(&self, id: window::Id) -> cosmic::prelude::Element<Self::Message> { | ||
if id == *access::ACCESS_ID { | ||
access::view(self).map(Msg::Access) | ||
} else if id == *screenshot::SCREENSHOT_ID { | ||
screenshot::view(self) | ||
} else { | ||
panic!("Unknown window id {:?}", id); | ||
} | ||
} | ||
|
||
fn update( | ||
&mut self, | ||
message: Self::Message, | ||
) -> cosmic::iced::Command<app::Message<Self::Message>> { | ||
match message { | ||
Msg::Access(m) => access::update_msg(self, m).map(cosmic::app::Message::App), | ||
Msg::Portal(e) => match e { | ||
subscription::Event::Access(args) => { | ||
access::update_args(self, args).map(cosmic::app::Message::App) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
fn subscription(&self) -> cosmic::iced_futures::Subscription<Self::Message> { | ||
subscription::portal_subscription().map(|e| Msg::Portal(e)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use i18n_embed::{ | ||
fluent::{fluent_language_loader, FluentLanguageLoader}, | ||
DefaultLocalizer, LanguageLoader, Localizer, | ||
}; | ||
use once_cell::sync::Lazy; | ||
use rust_embed::RustEmbed; | ||
|
||
#[derive(RustEmbed)] | ||
#[folder = "i18n/"] | ||
struct Localizations; | ||
|
||
pub static LANGUAGE_LOADER: Lazy<FluentLanguageLoader> = Lazy::new(|| { | ||
let loader: FluentLanguageLoader = fluent_language_loader!(); | ||
|
||
loader | ||
.load_fallback_language(&Localizations) | ||
.expect("Error while loading fallback language"); | ||
|
||
loader | ||
}); | ||
|
||
#[macro_export] | ||
macro_rules! fl { | ||
($message_id:literal) => {{ | ||
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id) | ||
}}; | ||
|
||
($message_id:literal, $($args:expr),*) => {{ | ||
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id, $($args), *) | ||
}}; | ||
} | ||
|
||
// Get the `Localizer` to be used for localizing this library. | ||
pub fn localizer() -> Box<dyn Localizer> { | ||
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations)) | ||
} | ||
|
||
pub fn localize() { | ||
let localizer = localizer(); | ||
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages(); | ||
|
||
if let Err(error) = localizer.select(&requested_languages) { | ||
log::error!("Error while loading language for Cosmic Portal {}", error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// contains the subscription which sends portal events and response channels to iced. | ||
|
||
use std::{ | ||
any::TypeId, | ||
fmt::{Debug, Formatter}, | ||
}; | ||
|
||
use cosmic::iced::subscription; | ||
use futures::{future, SinkExt}; | ||
use tokio::sync::mpsc::Receiver; | ||
use zbus::Connection; | ||
|
||
use crate::{ | ||
access::Access, screencast::ScreenCast, screenshot::Screenshot, wayland, DBUS_NAME, DBUS_PATH, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub enum Event { | ||
Access(crate::access::AccessDialogArgs), | ||
} | ||
|
||
impl Debug for Event { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Event::Access(args) => f | ||
.debug_struct("Access") | ||
.field("title", &args.title) | ||
.field("subtitle", &args.subtitle) | ||
.field("body", &args.body) | ||
.field("options", &args.options) | ||
.field("app_id", &args.app_id) | ||
.field("parent_window", &args.parent_window) | ||
.field("handle", &args.handle) | ||
.finish(), | ||
} | ||
} | ||
} | ||
|
||
pub enum State { | ||
Init, | ||
Waiting(Connection, Receiver<Event>), | ||
} | ||
|
||
pub(crate) fn portal_subscription() -> cosmic::iced::Subscription<Event> { | ||
subscription::channel(TypeId::of::<Event>(), 10, |mut output| async move { | ||
let mut state = State::Init; | ||
loop { | ||
if let Err(err) = process_changes(&mut state, &mut output).await { | ||
log::debug!("Portal Subscription Error: {:?}", err); | ||
} | ||
future::pending::<()>().await; | ||
} | ||
}) | ||
} | ||
|
||
pub(crate) async fn process_changes( | ||
state: &mut State, | ||
output: &mut futures::channel::mpsc::Sender<Event>, | ||
) -> anyhow::Result<()> { | ||
match state { | ||
State::Init => { | ||
let (tx, rx) = tokio::sync::mpsc::channel(10); | ||
let wayland_connection = wayland::connect_to_wayland(); | ||
let wayland_helper = wayland::WaylandHelper::new(wayland_connection); | ||
|
||
let _connection = zbus::ConnectionBuilder::session()? | ||
.name(DBUS_NAME)? | ||
.serve_at(DBUS_PATH, Access::new(wayland_helper.clone(), tx.clone()))? | ||
.serve_at(DBUS_PATH, Screenshot::new(wayland_helper.clone()))? | ||
.serve_at(DBUS_PATH, ScreenCast::new(wayland_helper))? | ||
.build() | ||
.await?; | ||
*state = State::Waiting(Connection::session().await.unwrap(), rx); | ||
} | ||
State::Waiting(_, rx) => { | ||
while let Some(event) = rx.recv().await { | ||
match event { | ||
Event::Access(args) => { | ||
output.send(Event::Access(args)).await.unwrap(); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
Ok(()) | ||
} |