refactor out into separate files

This commit is contained in:
hippoz 2022-07-27 00:26:37 +03:00
parent c676773a44
commit c601beb28e
Signed by: hippoz
GPG key ID: 7C52899193467641
9 changed files with 186 additions and 157 deletions

View file

@ -9,6 +9,9 @@ librsvg_dep = dependency('librsvg-2.0')
executable(
'filemanager',
'./src/SvgWidget.cpp',
'./src/FileButton.cpp',
'./src/DirectoryView.cpp',
'./src/main.cpp',
dependencies : [raven_dep, librsvg_dep]
)

30
src/DirectoryView.cpp Normal file
View file

@ -0,0 +1,30 @@
#include "DirectoryView.hpp"
#include "src/Forward.hpp"
DirectoryView::~DirectoryView() {}
void DirectoryView::update() {
window()->start_batch();
target()->clear_children();
for (const auto &entry : std::filesystem::directory_iterator(m_current_path)) {
auto button = m_target->add<FileButton>(entry.path().filename(), this);
}
window()->end_batch();
}
void DirectoryView::navigate(std::string path) {
m_current_path = path;
update();
}
void DirectoryView::navigate_relative(std::string path) {
m_current_path /= path;
update();
}
void DirectoryView::on_init() {
m_target = make_target();
m_target->set_layout<Raven::DocumentLayout>(12.0);
update();
set_did_init(true);
}

25
src/DirectoryView.hpp Normal file
View file

@ -0,0 +1,25 @@
#pragma once
#include <string>
#include <filesystem>
#include "raven/Widget.hpp"
#include "raven/ScrollContainer.hpp"
#include "raven/DocumentLayout.hpp"
#include "FileButton.hpp"
class DirectoryView : public Raven::ScrollContainer {
public:
DirectoryView()
: Raven::ScrollContainer() {}
~DirectoryView();
void update();
void navigate(std::string path);
void navigate_relative(std::string path);
protected:
void on_init();
private:
std::filesystem::path m_current_path { "/" };
std::shared_ptr<Raven::Widget> m_target;
};

36
src/FileButton.cpp Normal file
View file

@ -0,0 +1,36 @@
#include "FileButton.hpp"
#include "DirectoryView.hpp"
Raven::GenericStyle FileButton::style = Raven::GenericStyle {
pango_font_description_from_string("sans-serif"),
Raven::black6,
Raven::white3,
Raven::white2,
Raven::white1,
0.0,
true,
true
};
void FileButton::on_init() {
rect().set_width(96);
rect().set_height(118);
set_style(&style);
auto layout = set_layout<Raven::BoxLayout>(Raven::Direction::Vertical);
layout->slot_percent(100); // icon
layout->slot_pixel(24); // name (text)
add<SvgWidget>("folder-adwaita.svg");
auto label = add<Raven::Label>(m_name, Raven::PaintTextAlign::Center);
label->rect().set_max_width(rect().width());
label->rect().set_max_height(24);
set_did_init(true);
}
void FileButton::on_activation_update(Raven::ActivationUpdateEvent &event) {
if (event.activation_status() == false && m_directory_view) {
m_directory_view->navigate_relative(m_name);
}
}

28
src/FileButton.hpp Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include <memory>
#include <string>
#include "raven/Widget.hpp"
#include "raven/BoxLayout.hpp"
#include "raven/Label.hpp"
#include "Forward.hpp"
#include "SvgWidget.hpp"
class FileButton : public Raven::Widget {
public:
static Raven::GenericStyle style;
public:
FileButton(std::string name, DirectoryView* directory_view)
: Raven::Widget()
, m_directory_view(directory_view)
, m_name(name) {}
std::string &name() { return m_name; }
protected:
void on_init();
void on_activation_update(Raven::ActivationUpdateEvent &event);
private:
DirectoryView* m_directory_view;
std::string m_name;
};

5
src/Forward.hpp Normal file
View file

@ -0,0 +1,5 @@
#pragma once
class SvgWidget;
class FileButton;
class DirectoryView;

32
src/SvgWidget.cpp Normal file
View file

@ -0,0 +1,32 @@
#include "SvgWidget.hpp"
void SvgWidget::on_init() {
m_file = g_file_new_for_path(m_path.c_str());
m_handle = rsvg_handle_new_from_gfile_sync(m_file, RSVG_HANDLE_FLAGS_NONE, NULL, NULL);
set_style(&Raven::clear_widget_style);
if (!m_handle)
{
std::cerr << "could not load svg file: " << m_path << std::endl;
exit(EXIT_FAILURE);
}
set_did_init(true);
}
void SvgWidget::on_paint() {
RsvgRectangle viewport = {
.x = 0.0,
.y = 0.0,
.width = rect().width(),
.height = rect().height(),
};
rsvg_handle_render_document(m_handle, painter()->cairo()->cobj(), &viewport, NULL);
}
SvgWidget::~SvgWidget() {
g_object_unref(m_file);
g_object_unref(m_handle);
}

25
src/SvgWidget.hpp Normal file
View file

@ -0,0 +1,25 @@
#pragma once
#include <string>
#include "raven/Widget.hpp"
#include "librsvg/rsvg.h"
class SvgWidget : public Raven::Widget {
public:
SvgWidget(std::string path)
: Raven::Widget()
, m_path(path) {}
~SvgWidget();
std::string &path() { return m_path; }
void set_path(std::string path) { m_path = path; repaint(); }
protected:
void on_init();
void on_paint();
private:
std::string m_path;
RsvgHandle *m_handle;
GFile *m_file;
};

View file

@ -1,165 +1,10 @@
#include "GenericStyle.hpp"
#include "Painter.hpp"
#include "librsvg/rsvg.h"
#include "raven/Label.hpp"
#include "cairomm/refptr.h"
#include "cairomm/surface.h"
#include "raven/Window.hpp"
#include "raven/Widget.hpp"
#include "raven/Button.hpp"
#include "raven/Box.hpp"
#include "raven/Label.hpp"
#include "raven/Layout.hpp"
#include "raven/RGB.hpp"
#include "raven/DocumentLayout.hpp"
#include "raven/Events.hpp"
#include "raven/BoxLayout.hpp"
#include "raven/Box.hpp"
#include "raven/ScrollContainer.hpp"
#include "raven/Styles.hpp"
#include <bits/types/FILE.h>
#include <iostream>
#include <memory>
#include <string>
#include <filesystem>
class SvgWidget : public Raven::Widget {
public:
SvgWidget(std::string path)
: Raven::Widget()
, m_path(path) {}
~SvgWidget() {
g_object_unref(m_file);
g_object_unref(m_handle);
}
std::string &path() { return m_path; }
void set_path(std::string path) { m_path = path; repaint(); }
protected:
void on_init() {
m_file = g_file_new_for_path(m_path.c_str());
m_handle = rsvg_handle_new_from_gfile_sync(m_file, RSVG_HANDLE_FLAGS_NONE, NULL, NULL);
set_style(&Raven::clear_widget_style);
if (!m_handle)
{
std::cerr << "could not load svg file: " << m_path << std::endl;
exit(EXIT_FAILURE);
}
set_did_init(true);
}
void on_paint() {
RsvgRectangle viewport = {
.x = 0.0,
.y = 0.0,
.width = rect().width(),
.height = rect().height(),
};
rsvg_handle_render_document(m_handle, painter()->cairo()->cobj(), &viewport, NULL);
}
private:
std::string m_path;
RsvgHandle *m_handle;
GFile *m_file;
};
class FileButton : public Raven::Widget {
public:
static Raven::GenericStyle style;
public:
FileButton(std::string name)
: Raven::Widget()
, m_name(name) {}
std::function<void()> on_open { [](){} };
std::string &name() { return m_name; }
protected:
void on_init() {
rect().set_width(96);
rect().set_height(118);
set_style(&style);
auto layout = set_layout<Raven::BoxLayout>(Raven::Direction::Vertical);
layout->slot_percent(100); // icon
layout->slot_pixel(24); // name (text)
add<SvgWidget>("folder-adwaita.svg");
auto label = add<Raven::Label>(m_name, Raven::PaintTextAlign::Center);
label->rect().set_max_width(rect().width());
label->rect().set_max_height(24);
set_did_init(true);
}
void on_activation_update(Raven::ActivationUpdateEvent &event) {
if (event.activation_status() == false) {
on_open();
}
}
private:
std::string m_name;
};
Raven::GenericStyle FileButton::style = Raven::GenericStyle {
pango_font_description_from_string("sans-serif"),
Raven::black6,
Raven::white3,
Raven::white2,
Raven::white1,
0.0,
true,
true
};
class DirectoryView : public Raven::ScrollContainer {
public:
DirectoryView()
: Raven::ScrollContainer() {}
void update() {
window()->start_batch();
m_target->clear_children();
for (const auto &entry : std::filesystem::directory_iterator(m_current_path)) {
auto button = m_target->add<FileButton>(entry.path().filename());
button->on_open = [this, button]() {
navigate_relative(button->name());
};
}
window()->end_batch();
}
void navigate(std::string path) {
m_current_path = path;
update();
}
void navigate_relative(std::string path) {
m_current_path /= path;
update();
}
protected:
void on_init() {
m_target = make_target();
m_target->set_layout<Raven::DocumentLayout>(12.0);
update();
set_did_init(true);
}
private:
std::filesystem::path m_current_path { "/" };
std::shared_ptr<Raven::Widget> m_target;
};
#include "DirectoryView.hpp"
int main() {
Raven::Window window {};
window.spawn_window();
auto main_widget = window.set_main_widget<DirectoryView>();
window.set_main_widget<DirectoryView>();
window.run(true);
return 0;