65 lines
2.8 KiB
Text
65 lines
2.8 KiB
Text
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
module;
|
||
|
|
#include <systemd/sd-bus.h>
|
||
|
|
export module Nostro.Ui.Shell:Portal;
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Nostro.Ui;
|
||
|
|
import :Loop;
|
||
|
|
|
||
|
|
// The desktop portal on the session bus, for what an app in a sandbox may ask of the desktop without a permission of
|
||
|
|
// its own: how the system looks (dark or light, and its accent colour), and opening an address in the user's browser.
|
||
|
|
// Without a session bus or a portal nothing is known and nothing opens, and the app goes on as it is.
|
||
|
|
|
||
|
|
export namespace Nostro::Ui::Shell {
|
||
|
|
struct Appearance {
|
||
|
|
bool known = false; // the portal gave a colour scheme
|
||
|
|
bool dark = false;
|
||
|
|
std::optional<Color> accent; // none: the system has no accent colour, or says none
|
||
|
|
friend bool operator==(const Appearance&, const Appearance&) = default;
|
||
|
|
};
|
||
|
|
|
||
|
|
class Portal {
|
||
|
|
public:
|
||
|
|
// Connects to the session bus and reads the appearance, waiting up to `patience` for the portal's answer: a
|
||
|
|
// window drawn before it would open light and turn dark. An answer that comes later is told as a change.
|
||
|
|
explicit Portal(Loop& loop, std::chrono::milliseconds patience = std::chrono::milliseconds(1000));
|
||
|
|
~Portal();
|
||
|
|
Portal(const Portal&) = delete;
|
||
|
|
Portal& operator=(const Portal&) = delete;
|
||
|
|
|
||
|
|
bool Connected() const noexcept;
|
||
|
|
const Appearance& Current() const noexcept;
|
||
|
|
// The appearance changed, or was learnt after the start.
|
||
|
|
std::function<void(const Appearance&)> onAppearance;
|
||
|
|
|
||
|
|
// Asks the desktop to open an address with the user's own program for it. `then` hears whether the desktop
|
||
|
|
// took the request, from the loop; it hears false at once when there is no portal to ask.
|
||
|
|
void OpenUri(std::string_view uri, std::function<void(bool taken)> then);
|
||
|
|
|
||
|
|
private:
|
||
|
|
static std::int32_t OnSettings(sd_bus_message* reply, void* data, sd_bus_error* error);
|
||
|
|
static std::int32_t OnChanged(sd_bus_message* signal, void* data, sd_bus_error* error);
|
||
|
|
static std::int32_t OnOpened(sd_bus_message* reply, void* data, sd_bus_error* error);
|
||
|
|
// Takes ReadAll's answer in; true when it changed what is known.
|
||
|
|
bool TakeSettings(sd_bus_message* reply);
|
||
|
|
bool TakeSetting(std::string_view key, sd_bus_message* value);
|
||
|
|
void Told();
|
||
|
|
|
||
|
|
struct Opening {
|
||
|
|
Portal* portal = nullptr;
|
||
|
|
std::function<void(bool)> then;
|
||
|
|
sd_bus_slot* slot = nullptr;
|
||
|
|
};
|
||
|
|
|
||
|
|
Loop& loop;
|
||
|
|
sd_bus* bus = nullptr;
|
||
|
|
sd_bus_slot* changed = nullptr;
|
||
|
|
sd_bus_slot* late = nullptr; // a ReadAll the start did not wait out
|
||
|
|
std::list<Opening> openings;
|
||
|
|
Appearance current;
|
||
|
|
bool answered = false; // ReadAll's answer came, whatever it was
|
||
|
|
};
|
||
|
|
}
|