add clock to bar

This commit is contained in:
2025-12-08 23:41:34 +01:00
commit de6ece13fa
8 changed files with 214 additions and 0 deletions

58
.gitignore vendored Normal file
View File

@@ -0,0 +1,58 @@
# Compiled Object files
**/.DS_Store
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
**/cmake-build-debug
**/CMakeCache.txt
**/cmake_install.cmake
**/install_manifest.txt
**/CMakeFiles/
**/CTestTestfile.cmake
**/Makefile
**/*.cbp
**/CMakeScripts
**/compile_commands.json
## Local
.idea/*.xml
build/**/*
.cache/**/*
bin/*
test/test_runner
.vscode/*
.vscode/**/*
cmake-build-debug/**/*
.idea

30
CMakeLists.txt Normal file
View File

@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 4.2.0)
project(bar)
set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTKMM REQUIRED gtkmm-4.0)
pkg_check_modules(LAYERSHELL REQUIRED gtk4-layer-shell-0)
include_directories(${GTKMM_INCLUDE_DIRS} ${LAYERSHELL_INCLUDE_DIRS})
link_directories(${GTKMM_LIBRARY_DIRS} ${LAYERSHELL_LIBRARY_DIRS})
add_library(bar_lib)
target_sources(bar_lib
PUBLIC
src/bar.cpp
src/widgets/clock.cpp
)
include_directories(bar_lib PRIVATE
include
)
add_executable(bar main.cpp)
target_link_libraries(bar bar_lib ${GTKMM_LIBRARIES} ${LAYERSHELL_LIBRARIES})

19
include/bar.hpp Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include <gtkmm.h>
#include <gtk4-layer-shell/gtk4-layer-shell.h>
#include "widgets/clock.hpp"
class Bar : public Gtk::Window {
public:
Bar();
protected:
Clock clock;
Gtk::CenterBox main_box{};
void setup_ui();
void load_css();
};

View File

@@ -0,0 +1,5 @@
class IUpdatable {
public:
virtual ~IUpdatable() = default;
virtual bool onUpdate() = 0;
};

12
include/widgets/clock.hpp Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#include <gtkmm/label.h>
#include <gtkmm.h>
#include <gtk4-layer-shell/gtk4-layer-shell.h>
#include "interface/updateable.ipp"
class Clock : public Gtk::Label, public IUpdatable {
public:
bool onUpdate();
};

7
main.cpp Normal file
View File

@@ -0,0 +1,7 @@
#include "bar.hpp"
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create("org.example.mybar");
return app->make_window_and_run<Bar>(argc, argv);
}

68
src/bar.cpp Normal file
View File

@@ -0,0 +1,68 @@
#include <ctime>
#include <gtkmm/enums.h>
#include <gtkmm/label.h>
#include "bar.hpp"
#include "glibmm/main.h"
#include "sigc++/functors/mem_fun.h"
#include "widgets/clock.hpp"
Bar::Bar() {
gtk_layer_init_for_window(this->gobj());
gtk_layer_set_anchor(this->gobj(), GTK_LAYER_SHELL_EDGE_TOP, true);
gtk_layer_set_anchor(this->gobj(), GTK_LAYER_SHELL_EDGE_LEFT, true);
gtk_layer_set_anchor(this->gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, true);
gtk_layer_auto_exclusive_zone_enable(this->gobj());
set_child(main_box);
load_css();
setup_ui();
this->clock.onUpdate();
Glib::signal_timeout().connect(
sigc::mem_fun(
this->clock,
&Clock::onUpdate
),
1000
);
}
void Bar::setup_ui() {
Gtk::Box left_box{Gtk::Orientation::HORIZONTAL};
Gtk::Box center_box{Gtk::Orientation::HORIZONTAL};
Gtk::Box right_box{Gtk::Orientation::HORIZONTAL};
main_box.set_start_widget(left_box);
main_box.set_center_widget(center_box);
main_box.set_end_widget(right_box);
Gtk::Label labelLeft("labelLeft");
Gtk::Label labelRight("labelRight");
left_box.append(labelLeft);
center_box.append(clock);
right_box.append(labelRight);
}
void Bar::load_css() {
auto css_provider = Gtk::CssProvider::create();
css_provider->load_from_data(R"(
window { background-color: #222; color: #fff; }
#clock-label { font-weight: bold; font-family: monospace; }
)");
Gtk::StyleContext::add_provider_for_display(
Gdk::Display::get_default(),
css_provider,
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
);
}

15
src/widgets/clock.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include "widgets/clock.hpp"
#include <chrono>
#include <iomanip>
bool Clock::onUpdate() {
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::stringstream ss;
ss << std::put_time(std::localtime(&now), "%H:%M:%S");
set_text(ss.str());
return true;
}