another wrappers for GTK widgets

This commit is contained in:
Selim Mustafaev 2022-05-12 00:35:08 +03:00
parent 88d1126822
commit ff532f5ed9
15 changed files with 208 additions and 63 deletions

View File

@ -49,7 +49,7 @@ add_executable(autocat_gnome main.cpp
gtkpp/Application.cpp
gtkpp/Application.h
gtkpp/Window.cpp
gtkpp/Window.h gtkpp/Box.cpp gtkpp/Box.h gtkpp/Widget.cpp gtkpp/Widget.h gtkpp/Entry.cpp gtkpp/Entry.h)
gtkpp/Window.h gtkpp/Box.cpp gtkpp/Box.h gtkpp/Widget.cpp gtkpp/Widget.h gtkpp/Entry.cpp gtkpp/Entry.h gtkpp/Button.cpp gtkpp/Button.h gtkpp/Spinner.cpp gtkpp/Spinner.h gtkpp/HeaderBar.cpp gtkpp/HeaderBar.h)
target_link_libraries(autocat_gnome ${GTKMM_LIBRARIES}
${GLIBMM_LIBRARIES}

View File

@ -14,6 +14,7 @@ namespace gtkpp {
Application::Application(const std::string &id) {
_app = adw_application_new(id.c_str(), G_APPLICATION_FLAGS_NONE);
g_signal_connect(_app, "activate", G_CALLBACK(activateCallback), this);
}
int Application::run(int argc, char **argv) {
@ -21,7 +22,7 @@ namespace gtkpp {
}
void Application::onActivate(const std::function<void()>& callback) {
g_signal_connect(_app, "activate", G_CALLBACK(activateCallback), this);
_signalActivate.connect(callback);
}

View File

@ -4,7 +4,7 @@
#include "Box.h"
gtkpp::Box::Box(GtkOrientation orientation, int spacing) {
gtkpp::Box::Box(GtkOrientation orientation, int spacing) : Widget() {
_widget = gtk_box_new(orientation, spacing);
}

27
gtkpp/Button.cpp Normal file
View File

@ -0,0 +1,27 @@
//
// Created by selim on 11.05.2022.
//
#include "Button.h"
namespace gtkpp {
void clickedCallback(GtkButton*, void* data) {
auto button = reinterpret_cast<Button*>(data);
button->_signalClicked.emit();
}
Button::Button() : Widget() {
_widget = gtk_button_new();
g_signal_connect(_widget, "clicked", G_CALLBACK(clickedCallback), this);
}
void Button::setTitle(const std::string &title) {
gtk_button_set_label(GTK_BUTTON(_widget), title.c_str());
}
void Button::onClick(const std::function<void()> &callback) {
_signalClicked.connect(callback);
}
}

29
gtkpp/Button.h Normal file
View File

@ -0,0 +1,29 @@
//
// Created by selim on 11.05.2022.
//
#ifndef AUTOCAT_GNOME_BUTTON_H
#define AUTOCAT_GNOME_BUTTON_H
#include "Widget.h"
#include <string>
#include <sigc++/sigc++.h>
namespace gtkpp {
class Button: public Widget {
private:
sigc::signal<void()> _signalClicked;
private:
friend void clickedCallback(GtkButton*, void* data);
public:
Button();
void setTitle(const std::string& title);
void onClick(const std::function<void()>& callback);
};
}
#endif //AUTOCAT_GNOME_BUTTON_H

View File

@ -4,18 +4,32 @@
#include "Entry.h"
gtkpp::Entry::Entry() {
_widget = gtk_entry_new();
namespace gtkpp {
void changedCallback(GtkEntry* widget, void* data) {
auto entry = reinterpret_cast<Entry*>(data);
entry->_signalChanged.emit();
}
void gtkpp::Entry::setPlaceholder(const std::string &placeholder) {
Entry::Entry() : Widget() {
_widget = gtk_entry_new();
g_signal_connect(_widget, "changed", G_CALLBACK(changedCallback), this);
}
void Entry::setPlaceholder(const std::string &placeholder) {
gtk_entry_set_placeholder_text(GTK_ENTRY(_widget), placeholder.c_str());
}
void gtkpp::Entry::setPurpose(GtkInputPurpose purpose) {
void Entry::setPurpose(GtkInputPurpose purpose) {
gtk_entry_set_input_purpose(GTK_ENTRY(_widget), purpose);
}
void gtkpp::Entry::setVisibility(bool visibility) {
void Entry::setVisibility(bool visibility) {
gtk_entry_set_visibility(GTK_ENTRY(_widget), visibility);
}
void Entry::onChanged(const std::function<void()>& callback) {
_signalChanged.connect(callback);
}
}

View File

@ -7,17 +7,23 @@
#include "Widget.h"
#include <string>
#include <sigc++/sigc++.h>
namespace gtkpp {
class Entry: public Widget {
private:
sigc::signal<void()> _signalChanged;
private:
friend void changedCallback(GtkEntry* widget, void* data);
public:
Entry();
void setPlaceholder(const std::string& placeholder);
void setPurpose(GtkInputPurpose purpose);
void setVisibility(bool visibility);
void onChanged(const std::function<void()>& callback);
};
}

21
gtkpp/HeaderBar.cpp Normal file
View File

@ -0,0 +1,21 @@
//
// Created by selim on 11.05.2022.
//
#include "HeaderBar.h"
#include <adwaita.h>
namespace gtkpp {
HeaderBar::HeaderBar() : Widget() {
_widget = adw_header_bar_new();
}
HeaderBar::HeaderBar(const std::string &title): HeaderBar() {
setTitle(title);
}
void HeaderBar::setTitle(const std::string& title) {
adw_header_bar_set_title_widget(ADW_HEADER_BAR(_widget), gtk_label_new(title.c_str()));
}
}

22
gtkpp/HeaderBar.h Normal file
View File

@ -0,0 +1,22 @@
//
// Created by selim on 11.05.2022.
//
#ifndef AUTOCAT_GNOME_HEADERBAR_H
#define AUTOCAT_GNOME_HEADERBAR_H
#include "Widget.h"
#include <string>
namespace gtkpp {
class HeaderBar: public Widget {
public:
HeaderBar();
explicit HeaderBar(const std::string& title);
void setTitle(const std::string& title);
};
}
#endif //AUTOCAT_GNOME_HEADERBAR_H

13
gtkpp/Spinner.cpp Normal file
View File

@ -0,0 +1,13 @@
//
// Created by selim on 11.05.2022.
//
#include "Spinner.h"
namespace gtkpp {
Spinner::Spinner() : Widget() {
_widget = gtk_spinner_new();
}
}

19
gtkpp/Spinner.h Normal file
View File

@ -0,0 +1,19 @@
//
// Created by selim on 11.05.2022.
//
#ifndef AUTOCAT_GNOME_SPINNER_H
#define AUTOCAT_GNOME_SPINNER_H
#include "Widget.h"
namespace gtkpp {
class Spinner: public Widget {
public:
Spinner();
};
}
#endif //AUTOCAT_GNOME_SPINNER_H

View File

@ -22,3 +22,17 @@ void gtkpp::Widget::setVAlign(GtkAlign align) {
void gtkpp::Widget::setVExpand(bool expand) {
gtk_widget_set_vexpand(_widget, expand);
}
void gtkpp::Widget::setHorizontalMargins(int margin) {
gtk_widget_set_margin_start(_widget, margin);
gtk_widget_set_margin_end(_widget, margin);
}
void gtkpp::Widget::setVerticalMargins(int margin) {
gtk_widget_set_margin_top(_widget, margin);
gtk_widget_set_margin_bottom(_widget, margin);
}
void gtkpp::Widget::setEnabled(bool enabled) {
gtk_widget_set_sensitive(_widget, enabled);
}

View File

@ -16,8 +16,11 @@ namespace gtkpp {
public:
[[nodiscard]] GtkWidget* gobj() const;
void setMargins(int margin);
void setVerticalMargins(int margin);
void setHorizontalMargins(int margin);
void setVAlign(GtkAlign align);
void setVExpand(bool expand);
void setEnabled(bool enabled);
};
}

View File

@ -7,70 +7,44 @@
#include "../services/Api.h"
#include "../coro/GLibMainContextExecutor.h"
#include "../gtkpp/HeaderBar.h"
#include <iostream>
#include <folly/experimental/coro/Task.h>
#include <folly/executors/IOThreadPoolExecutor.h>
LoginWindow::LoginWindow(): _rootBox(GTK_ORIENTATION_VERTICAL, 0),
_contentBox(GTK_ORIENTATION_VERTICAL, 8) {
LoginWindow::LoginWindow() {
setDefaultSize(640, 480);
auto header = adw_header_bar_new();
adw_header_bar_set_title_widget(ADW_HEADER_BAR(header), gtk_label_new("Login"));
auto loginButton = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(loginButton), "Log in");
gtk_widget_set_margin_top(loginButton, 8);
gtk_widget_set_margin_bottom(loginButton, 8);
gtk_widget_set_sensitive(loginButton, false);
auto spinner = gtk_spinner_new();
_loginEntry.setPlaceholder("Email");
_loginEntry.setPurpose(GTK_INPUT_PURPOSE_EMAIL);
_loginEntry.onChanged([this] { validateFields(); });
_passwordEntry.setPlaceholder("Password");
_passwordEntry.setPurpose(GTK_INPUT_PURPOSE_PASSWORD);
_passwordEntry.setVisibility(false);
_passwordEntry.onChanged([this] { validateFields(); });
_contentBox.append(_loginEntry);
_contentBox.append(_passwordEntry);
_contentBox.append(loginButton);
_contentBox.append(spinner);
_contentBox.setMargins(48);
_contentBox.setVAlign(GTK_ALIGN_CENTER);
_contentBox.setVExpand(true);
_loginButton.setTitle("Log in");
_loginButton.setVerticalMargins(8);
_loginButton.setEnabled(false);
_loginButton.onClick([this] { loginClicked(); });
_rootBox.append(header);
_rootBox.append(_contentBox);
gtkpp::Box contentBox(GTK_ORIENTATION_VERTICAL, 8);
contentBox.append(_loginEntry);
contentBox.append(_passwordEntry);
contentBox.append(_loginButton);
contentBox.append(_spinner);
contentBox.setMargins(48);
contentBox.setVAlign(GTK_ALIGN_CENTER);
contentBox.setVExpand(true);
adw_window_set_content(ADW_WINDOW(_window), _rootBox.gobj());
gtkpp::Box rootBox(GTK_ORIENTATION_VERTICAL, 0);
rootBox.append(gtkpp::HeaderBar("Login"));
rootBox.append(contentBox);
// _emailField.set_placeholder_text("Email");
// _passwordField.set_placeholder_text("Password");
// _passwordField.set_input_purpose(Gtk::InputPurpose::PASSWORD);
// _passwordField.set_visibility(false);
//
// _emailField.signal_changed().connect(sigc::mem_fun(*this, &LoginWindow::validateFields));
// _passwordField.signal_changed().connect(sigc::mem_fun(*this, &LoginWindow::validateFields));
//
// _loginButton.set_margin_top(8);
// _loginButton.set_margin_bottom(8);
// _loginButton.set_label("Log in");
// _loginButton.signal_clicked().connect(sigc::mem_fun(*this, &LoginWindow::loginClicked));
// _loginButton.set_sensitive(false);
//
// Gtk::Box box(Gtk::Orientation::VERTICAL, 8);
// box.set_margin(48);
// box.set_valign(Gtk::Align::CENTER);
//
// box.append(_emailField);
// box.append(_passwordField);
// box.append(_loginButton);
// box.append(_spinner);
//
// set_child(box);
adw_window_set_content(ADW_WINDOW(_window), rootBox.gobj());
}
void LoginWindow::loginClicked() {

View File

@ -9,6 +9,8 @@
#include "../gtkpp/Window.h"
#include "../gtkpp/Box.h"
#include "../gtkpp/Entry.h"
#include "../gtkpp/Button.h"
#include "../gtkpp/Spinner.h"
class LoginWindow: public gtkpp::Window {
private:
@ -18,10 +20,10 @@ private:
// Gtk::Spinner _spinner;
// std::unique_ptr<Gtk::MessageDialog> _dialog;
gtkpp::Box _rootBox;
gtkpp::Box _contentBox;
gtkpp::Entry _loginEntry;
gtkpp::Entry _passwordEntry;
gtkpp::Button _loginButton;
gtkpp::Spinner _spinner;
public:
LoginWindow();