aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 861796c0452e8d2354d78b468651c7a98e379989 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "../include/Window.hpp"
#include <gtkmm/application.h>
#include <gtkmm/cssprovider.h>
#include <gtkmm/settings.h>
#ifndef NDEBUG
#include <giomm.h>
#endif

// TODO: When creating a room, immediately switch to it.
// TODO: When changing nickname or avatar, do not merge next message with previous one even if next message is sent right after the previous one.
// We want to see user changes immediately.

static int setWindowCss(dchat::Window &window, Glib::RefPtr<Gtk::CssProvider> css)
{
    auto ctx = window.get_style_context();
    auto screen = Gdk::Screen::get_default();
    ctx->remove_provider_for_screen(screen, css);

    try
    {
        if(!css->load_from_path("css/style.css"))
        {
            fprintf(stderr, "Failed to load css/style.css");
            return 1;
        }
    }
    catch(Gtk::CssProviderError &e)
    {
        fprintf(stderr, "Failed to load css/style.css, error: %s\n", e.what().c_str());
        return 1;
    }

    ctx->add_provider_for_screen(screen, css, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    return 0;
}

int main (int argc, char *argv[])
{
    char *nativeStyle = std::getenv("NATIVE_STYLE");
    bool useNativeStyle = nativeStyle && strcmp(nativeStyle, "1") == 0;
    auto app = Gtk::Application::create(argc, argv, "dec05eba.dchat", Gio::APPLICATION_NON_UNIQUE);
    auto css = Gtk::CssProvider::create();
    dchat::Window window;
    if(!useNativeStyle)
    {
        if(setWindowCss(window, css) != 0)
            return 1;

#ifndef NDEBUG
        auto cssFile = Gio::File::create_for_path("css/style.css");
        auto cssFileMonitor = cssFile->monitor_file();
        cssFileMonitor->signal_changed().connect([&window, css](const Glib::RefPtr<Gio::File> &file, const Glib::RefPtr<Gio::File> &otherFile, Gio::FileMonitorEvent event)
        {
            fprintf(stderr, "Css file modified, reloading\n");
            setWindowCss(window, css);
        });
#endif
    }

    return app->run(window);
}