status indicator sets window icon
Some checks are pending
ContinuousDelivery / linux-ubuntu (push) Waiting to run
ContinuousDelivery / android (map[ndk_abi:arm64-v8a vcpkg_toolkit:arm64-android]) (push) Waiting to run
ContinuousDelivery / android (map[ndk_abi:armeabi-v7a vcpkg_toolkit:arm-neon-android]) (push) Waiting to run
ContinuousDelivery / android (map[ndk_abi:x86_64 vcpkg_toolkit:x64-android]) (push) Waiting to run
ContinuousDelivery / windows (push) Waiting to run
ContinuousDelivery / windows-asan (push) Waiting to run
ContinuousDelivery / release (push) Blocked by required conditions
ContinuousIntegration / linux (push) Waiting to run
ContinuousIntegration / android (map[ndk_abi:arm64-v8a vcpkg_toolkit:arm64-android]) (push) Waiting to run
ContinuousIntegration / android (map[ndk_abi:armeabi-v7a vcpkg_toolkit:arm-neon-android]) (push) Waiting to run
ContinuousIntegration / android (map[ndk_abi:x86_64 vcpkg_toolkit:x64-android]) (push) Waiting to run
ContinuousIntegration / macos (push) Waiting to run
ContinuousIntegration / windows (push) Waiting to run

This commit is contained in:
Green Sky 2025-01-09 16:24:59 +01:00
parent b226e9436d
commit 59df8c6748
No known key found for this signature in database
5 changed files with 111 additions and 0 deletions

View File

@ -105,6 +105,9 @@ target_sources(tomato PUBLIC
./tox_friend_faux_offline_messaging.hpp
./tox_friend_faux_offline_messaging.cpp
./status_indicator.hpp
./status_indicator.cpp
./chat_gui4.hpp
./chat_gui4.cpp

View File

@ -43,6 +43,7 @@ MainScreen::MainScreen(SimpleConfigModel&& conf_, SDL_Renderer* renderer_, Theme
contact_tc(tal, sdlrtu),
mil(),
msg_tc(mil, sdlrtu),
si(rmm, cr, SDL_GetRenderWindow(renderer_)),
cg(conf, os, rmm, cr, sdlrtu, contact_tc, msg_tc, theme),
sw(conf),
osui(os),

View File

@ -29,6 +29,7 @@
#include "./tox_avatar_loader.hpp"
#include "./message_image_loader.hpp"
#include "./status_indicator.hpp"
#include "./chat_gui4.hpp"
#include "./chat_gui/settings_window.hpp"
#include "./object_store_ui.hpp"
@ -91,6 +92,7 @@ struct MainScreen final : public Screen {
MessageImageLoader mil;
TextureCache<void*, Message3Handle, MessageImageLoader> msg_tc;
StatusIndicator si;
ChatGui4 cg;
SettingsWindow sw;
ObjectStoreUI osui;

69
src/status_indicator.cpp Normal file
View File

@ -0,0 +1,69 @@
#include "./status_indicator.hpp"
#include <solanaceae/contact/components.hpp>
#include <solanaceae/message3/components.hpp>
#include "./icon_generator.hpp"
#include <memory>
void StatusIndicator::updateState(State state) {
if (_state == state) {
return;
}
_state = state;
std::unique_ptr<SDL_Surface, decltype(&SDL_DestroySurface)> surf{nullptr, &SDL_DestroySurface};
switch (state) {
case State::base:
surf = {IconGenerator::base(), &SDL_DestroySurface};
break;
case State::unread:
surf = {IconGenerator::colorIndicator(1.f, .5f, 0.f, 1.f), &SDL_DestroySurface};
break;
case State::none:
break;
}
SDL_SetWindowIcon(_main_window, surf.get());
}
StatusIndicator::StatusIndicator(
RegistryMessageModelI& rmm,
Contact3Registry& cr,
SDL_Window* main_window
) :
_rmm(rmm),
_cr(cr),
_main_window(main_window)
{
// start off with base icon
updateState(State::base);
}
void StatusIndicator::render(float delta) {
_cooldown -= delta;
if (_cooldown <= 0.f) {
_cooldown = 1.f; // once a second
bool has_unread = false;
for (const auto& c : _cr.view<Contact::Components::TagBig>()) {
// maybe cache mm?
if (const auto* mm = _rmm.get(c); mm != nullptr) {
if (const auto* unread_storage = mm->storage<Message::Components::TagUnread>(); unread_storage != nullptr && !unread_storage->empty()) {
has_unread = true;
break;
}
}
}
if (has_unread) {
updateState(State::unread);
} else {
updateState(State::base);
}
}
}

36
src/status_indicator.hpp Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#include <solanaceae/message3/registry_message_model.hpp>
#include <SDL3/SDL.h>
// service that sets window and tray icon depending on program state
class StatusIndicator {
RegistryMessageModelI& _rmm;
Contact3Registry& _cr;
SDL_Window* _main_window;
// systray ptr here
float _cooldown {1.f};
enum class State {
base,
unread,
none, // only used for initialization
} _state = State::none;
void updateState(State state);
public:
StatusIndicator(
RegistryMessageModelI& rmm,
Contact3Registry& cr,
SDL_Window* main_window
);
// does not actually render, just on the render thread
void render(float delta);
};