Skip to content

Commit

Permalink
refactor: single instance
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Nov 22, 2023
1 parent a0a11b6 commit 791441c
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 196 deletions.
261 changes: 161 additions & 100 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ edition = "2021"
[dependencies]
anyhow = "1.0.75"
calloop = "0.12.3"
cctk = { package = "cosmic-client-toolkit", git = "https://github.com/pop-os/cosmic-protocols" }
clap = { version = "4", features = ["derive"] }
cosmic-comp-config = { git = "https://github.com/pop-os/cosmic-comp" }
env_logger = "0.10.0"
futures-channel = "0.3.25"
gbm = "0.14.0"
libcosmic = { git = "https://github.com/pop-os/libcosmic", default-features = false, features = ["tokio", "wayland"] }
libcosmic = { git = "https://github.com/pop-os/libcosmic", default-features = false, features = ["tokio", "wayland", "single-instance"] }
cosmic-config = { git = "https://github.com/pop-os/libcosmic" }
memmap2 = "0.9.0"
tokio = "1.23.0"
Expand Down
1 change: 1 addition & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Build-Depends:
cargo,
libegl1-mesa-dev,
libgbm-dev,
libinput-dev,
libudev-dev,
libxkbcommon-dev,
libwayland-dev,
Expand Down
125 changes: 88 additions & 37 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use cctk::{
Connection, Proxy, WEnum,
},
};
use clap::Parser;
use cosmic::{
app::{Application, CosmicFlags, DbusActivationDetails, Message},
cctk,
iced::{
self,
event::wayland::{Event as WaylandEvent, OutputEvent},
Expand All @@ -22,18 +25,15 @@ use cosmic::{
actions::data_device::{DataFromMimeType, DndIcon},
data_device::{accept_mime_type, set_actions, start_drag},
},
widget, Application, Command, Size, Subscription,
widget, Command, Size, Subscription,
},
iced_runtime::{
command::platform_specific::wayland::layer_surface::{
IcedOutput, SctkLayerSurfaceSettings,
},
window::Id as SurfaceId,
},
iced_sctk::{
commands::layer_surface::{destroy_layer_surface, get_layer_surface},
settings::InitialSurface,
},
iced_sctk::commands::layer_surface::{destroy_layer_surface, get_layer_surface},
};
use cosmic_config::ConfigGet;
use std::{
Expand All @@ -43,12 +43,34 @@ use std::{

// accept_mime_type, finish_dnd, request_dnd_data, set_actions,

mod toggle_dbus;
mod view;
mod wayland;

static WORKSPACE_MIME: &str = "text/x.cosmic-workspace-id";

#[derive(Parser, Debug, Clone)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Args {}

#[derive(Default, Debug, Clone)]
pub struct WorkspaceCommands;

impl ToString for WorkspaceCommands {
fn to_string(&self) -> String {
String::new()
}
}

impl CosmicFlags for Args {
type SubCommand = WorkspaceCommands;
type Args = Vec<String>;

fn action(&self) -> Option<&WorkspaceCommands> {
None
}
}

struct WorkspaceDndId(String);

impl DataFromMimeType for WorkspaceDndId {
Expand All @@ -71,7 +93,6 @@ enum Msg {
CloseWorkspace(zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1),
ActivateToplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
CloseToplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
DBus(toggle_dbus::Event),
StartDrag(Size, DragSurface),
SourceFinished,
DndWorkspaceEnter(DndAction, Vec<String>, (f32, f32)),
Expand Down Expand Up @@ -152,6 +173,7 @@ struct App {
wayland_cmd_sender: Option<calloop::channel::Sender<wayland::Cmd>>,
drag_surface: Option<(SurfaceId, DragSurface, Size)>,
conf: Conf,
core: cosmic::app::Core,
}

impl App {
Expand Down Expand Up @@ -181,7 +203,10 @@ impl App {
self.toplevels.iter_mut().find(|i| &i.handle == handle)
}

fn create_surface(&mut self, output: wl_output::WlOutput) -> Command<Msg> {
fn create_surface(
&mut self,
output: wl_output::WlOutput,
) -> Command<cosmic::app::Message<Msg>> {
let id = self.next_surface_id();
self.layer_surfaces.insert(
id,
Expand All @@ -201,7 +226,10 @@ impl App {
})
}

fn destroy_surface(&mut self, output: &wl_output::WlOutput) -> Command<Msg> {
fn destroy_surface(
&mut self,
output: &wl_output::WlOutput,
) -> Command<cosmic::app::Message<Msg>> {
if let Some((id, _)) = self
.layer_surfaces
.iter()
Expand All @@ -215,15 +243,15 @@ impl App {
}
}

fn toggle(&mut self) -> Command<Msg> {
fn toggle(&mut self) -> Command<cosmic::app::Message<Msg>> {
if self.visible {
self.hide()
} else {
self.show()
}
}

fn show(&mut self) -> Command<Msg> {
fn show(&mut self) -> Command<cosmic::app::Message<Msg>> {
if !self.visible {
self.visible = true;
let outputs = self.outputs.clone();
Expand All @@ -241,7 +269,7 @@ impl App {
}

// Close all shell surfaces
fn hide(&mut self) -> Command<Msg> {
fn hide(&mut self) -> Command<cosmic::app::Message<Msg>> {
self.visible = false;
self.update_capture_filter();
Command::batch(
Expand Down Expand Up @@ -273,22 +301,25 @@ impl App {

impl Application for App {
type Message = Msg;
type Theme = cosmic::Theme;
type Executor = iced::executor::Default;
type Flags = ();

fn new(_flags: ()) -> (Self, Command<Msg>) {
(Self::default(), Command::none())
}

fn title(&self) -> String {
String::from("cosmic-workspaces")
type Flags = Args;
const APP_ID: &'static str = "com.system76.CosmicWorkspaces";

fn init(
core: cosmic::app::Core,
_flags: Self::Flags,
) -> (Self, iced::Command<Message<Self::Message>>) {
(
Self {
core,
..Default::default()
},
Command::none(),
)
}

// TODO transparent style?
// TODO: show panel and dock? Drag?

fn update(&mut self, message: Msg) -> Command<Msg> {
fn update(&mut self, message: Msg) -> Command<cosmic::app::Message<Msg>> {
match message {
Msg::WaylandEvent(evt) => match evt {
WaylandEvent::Output(evt, output) => {
Expand Down Expand Up @@ -443,9 +474,6 @@ impl Application for App {
toplevel_manager.close(&toplevel_handle);
}
}
Msg::DBus(toggle_dbus::Event::Toggle) => {
return self.toggle();
}
Msg::StartDrag(size, drag_surface) => {
match &drag_surface {
DragSurface::Workspace { output, name: _ } => {
Expand Down Expand Up @@ -487,6 +515,16 @@ impl Application for App {

Command::none()
}
fn dbus_activation(
&mut self,
msg: cosmic::app::DbusActivationMessage,
) -> iced::Command<cosmic::app::Message<Self::Message>> {
if let DbusActivationDetails::Activate = msg.msg {
self.toggle()
} else {
Command::none()
}
}

fn subscription(&self) -> Subscription<Msg> {
let events = iced::subscription::events_with(|evt, _| {
Expand All @@ -503,14 +541,18 @@ impl Application for App {
None
}
});
let mut subscriptions = vec![events, toggle_dbus::subscription().map(Msg::DBus)];
let mut subscriptions = vec![events];
if let Some(conn) = self.conn.clone() {
subscriptions.push(wayland::subscription(conn).map(Msg::Wayland));
}
iced::Subscription::batch(subscriptions)
}

fn view(&self, id: SurfaceId) -> cosmic::Element<Msg> {
fn view(&self) -> cosmic::prelude::Element<Self::Message> {
unreachable!()
}

fn view_window(&self, id: iced::window::Id) -> cosmic::prelude::Element<Self::Message> {
use iced::widget::*;
if let Some(surface) = self.layer_surfaces.get(&id) {
return view::layer_surface(self, surface);
Expand All @@ -534,18 +576,27 @@ impl Application for App {
text("workspaces").into()
}

fn close_requested(&self, id: SurfaceId) -> Msg {
Msg::Closed(id)
fn on_close_requested(&self, id: SurfaceId) -> Option<Msg> {
Some(Msg::Closed(id))
}

fn core(&self) -> &cosmic::app::Core {
&self.core
}

fn core_mut(&mut self) -> &mut cosmic::app::Core {
&mut self.core
}
}

pub fn main() -> iced::Result {
env_logger::init();

App::run(iced::Settings {
antialiasing: true,
exit_on_close_request: false,
initial_surface: InitialSurface::None,
..iced::Settings::default()
})
cosmic::app::run_single_instance::<App>(
cosmic::app::Settings::default()
.antialiasing(true)
.no_main_window(true)
.exit_on_close(false),
Args::parse(),
)
}
46 changes: 0 additions & 46 deletions src/toggle_dbus.rs

This file was deleted.

23 changes: 13 additions & 10 deletions src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cctk::{
wayland_client::protocol::wl_output,
};
use cosmic::{
cctk,
iced::{
self,
widget::{column, row},
Expand Down Expand Up @@ -36,17 +37,19 @@ pub(crate) fn layer_surface<'a>(
})
}));
match layout {
WorkspaceLayout::Vertical => row![sidebar, toplevels]
.spacing(12)
.height(iced::Length::Fill)
.width(iced::Length::Fill)
.into(),
WorkspaceLayout::Horizontal => column![sidebar, toplevels]
.spacing(12)
.height(iced::Length::Fill)
.width(iced::Length::Fill)
.into(),
WorkspaceLayout::Vertical => widget::cosmic_container::container(
row![sidebar, toplevels]
.spacing(12)
.height(iced::Length::Fill)
.width(iced::Length::Fill),
),
WorkspaceLayout::Horizontal => widget::cosmic_container::container(
column![sidebar, toplevels]
.spacing(12)
.height(iced::Length::Fill),
),
}
.into()
}

fn close_button(on_press: Msg) -> cosmic::Element<'static, Msg> {
Expand Down
1 change: 1 addition & 0 deletions src/wayland/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cctk::{
Connection, Dispatch, QueueHandle, WEnum,
},
};
use cosmic::cctk;
use cosmic::iced::widget::image;
use std::{
os::fd::{AsFd, OwnedFd},
Expand Down
1 change: 1 addition & 0 deletions src/wayland/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cctk::{
screencopy::{ScreencopySessionData, ScreencopySessionDataExt},
wayland_client::{protocol::wl_output, Proxy, QueueHandle},
};
use cosmic::cctk;

use std::sync::{
atomic::{AtomicBool, Ordering},
Expand Down
1 change: 1 addition & 0 deletions src/wayland/dmabuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cctk::{
},
wayland_client::{protocol::wl_buffer, Connection, QueueHandle},
};
use cosmic::cctk;

use std::{fs, io, os::unix::fs::MetadataExt, path::PathBuf};

Expand Down
Loading

0 comments on commit 791441c

Please sign in to comment.