Splitting the section content from the main_view logic.

This commit is contained in:
sergiotarxz 2021-11-30 22:38:48 +01:00
parent ca63ee7449
commit f5b29029a4
5 changed files with 70 additions and 40 deletions

View File

@ -1,7 +1,10 @@
#pragma once #pragma once
#include <glib.h>
typedef struct { typedef struct {
AdwHeaderBar *header; AdwHeaderBar *header;
AdwLeaflet *views_leaflet; AdwLeaflet *views_leaflet;
GtkButton *previous; GtkButton *previous;
gboolean is_set_previous;
} ControlsAdwaita; } ControlsAdwaita;

View File

@ -0,0 +1,4 @@
#pragma once
GtkWidget *
create_explore_view (ControlsAdwaita *controls);

View File

@ -29,6 +29,7 @@ sources = [
'src/backend/readmng.c', 'src/backend/readmng.c',
'src/database.c', 'src/database.c',
'src/database/statement.c', 'src/database/statement.c',
'src/view/explore.c',
'src/main.c', 'src/main.c',
] ]

57
src/view/explore.c Normal file
View File

@ -0,0 +1,57 @@
#include <adwaita.h>
#include <gtk/gtk.h>
#include <openmg/view/list_view_manga.h>
#include <openmg/view/controls.h>
#include <openmg/view/explore.h>
#include <openmg/backend/readmng.h>
#include <openmg/manga.h>
static void
hide_main_view (GtkWidget *main_view,
gpointer user_data);
static void
show_main_view (GtkWidget *main_view,
gpointer user_data);
GtkWidget *
create_explore_view (ControlsAdwaita *controls) {
GtkListView *list_view;
GListStore *mangas;
GtkWidget *scroll = gtk_scrolled_window_new ();
MgBackendReadmng *readmng = mg_backend_readmng_new ();
mangas = mg_backend_readmng_get_featured_manga (readmng);
g_signal_connect (scroll, "map", G_CALLBACK (show_main_view), controls);
g_signal_connect (scroll, "unmap", G_CALLBACK (hide_main_view), controls);
list_view = create_list_view_mangas (mangas, controls);
gtk_widget_set_valign (scroll, GTK_ALIGN_FILL);
gtk_widget_set_vexpand (scroll, 1);
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scroll), GTK_WIDGET (list_view));
g_clear_object (&readmng);
return scroll;
}
static void
show_main_view (GtkWidget *main_view,
gpointer user_data) {
ControlsAdwaita *controls = (ControlsAdwaita *) user_data;
GtkWidget *previous = GTK_WIDGET (controls->previous);
AdwHeaderBar *header = controls->header;
if (controls->is_set_previous) {
adw_header_bar_remove (header, previous);
controls->is_set_previous = 0;
}
}
static void
hide_main_view (GtkWidget *main_view,
gpointer user_data) {
ControlsAdwaita *controls = (ControlsAdwaita *) user_data;
GtkWidget *previous = GTK_WIDGET (controls->previous);
AdwHeaderBar *header = controls->header;
if (!controls->is_set_previous) {
adw_header_bar_pack_start (header, previous);
controls->is_set_previous = 1;
}
}

View File

@ -3,10 +3,8 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <adwaita.h> #include <adwaita.h>
#include <openmg/backend/readmng.h>
#include <openmg/manga.h>
#include <openmg/view/controls.h> #include <openmg/view/controls.h>
#include <openmg/view/list_view_manga.h> #include <openmg/view/explore.h>
static AdwHeaderBar * static AdwHeaderBar *
create_headerbar (GtkBox *box, AdwLeaflet *views_leaflet, GtkButton **out_previous); create_headerbar (GtkBox *box, AdwLeaflet *views_leaflet, GtkButton **out_previous);
@ -14,12 +12,6 @@ static GtkBox *
create_main_box (AdwApplicationWindow *window); create_main_box (AdwApplicationWindow *window);
static void static void
go_back_view (GtkButton *previous, gpointer user_data); go_back_view (GtkButton *previous, gpointer user_data);
static void
hide_main_view (GtkWidget *main_view,
gpointer user_data);
static void
show_main_view (GtkWidget *main_view,
gpointer user_data);
static void static void
activate (AdwApplication *app, activate (AdwApplication *app,
@ -30,10 +22,7 @@ activate (AdwApplication *app,
GtkBox *box = create_main_box( GtkBox *box = create_main_box(
ADW_APPLICATION_WINDOW ADW_APPLICATION_WINDOW
(window)); (window));
GListStore *mangas; GtkWidget *explore_view;
MgBackendReadmng *readmng = mg_backend_readmng_new ();
GtkListView *list_view;
GtkWidget *scroll;
AdwLeaflet *views_leaflet = ADW_LEAFLET (adw_leaflet_new ()); AdwLeaflet *views_leaflet = ADW_LEAFLET (adw_leaflet_new ());
ControlsAdwaita *controls = g_malloc (sizeof *controls); ControlsAdwaita *controls = g_malloc (sizeof *controls);
GtkButton *previous = NULL; GtkButton *previous = NULL;
@ -53,24 +42,16 @@ activate (AdwApplication *app,
controls->header = header_bar; controls->header = header_bar;
controls->views_leaflet = views_leaflet; controls->views_leaflet = views_leaflet;
controls->previous = previous; controls->previous = previous;
controls->is_set_previous = 0;
mangas = mg_backend_readmng_get_featured_manga (readmng); explore_view = create_explore_view (controls);
list_view = create_list_view_mangas (mangas, controls);
scroll = gtk_scrolled_window_new ();
g_signal_connect (scroll, "map", G_CALLBACK (show_main_view), controls);
g_signal_connect (scroll, "unmap", G_CALLBACK (hide_main_view), controls);
gtk_widget_set_valign (scroll, GTK_ALIGN_FILL); adw_leaflet_append (views_leaflet, explore_view);
gtk_widget_set_vexpand (scroll, 1);
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scroll), GTK_WIDGET (list_view));
adw_leaflet_append (views_leaflet, scroll);
adw_leaflet_set_can_unfold (views_leaflet, false); adw_leaflet_set_can_unfold (views_leaflet, false);
gtk_box_append (box, GTK_WIDGET (views_leaflet)); gtk_box_append (box, GTK_WIDGET (views_leaflet));
gtk_widget_show (window); gtk_widget_show (window);
g_clear_object (&readmng);
} }
static GtkBox * static GtkBox *
@ -84,23 +65,7 @@ create_main_box (AdwApplicationWindow *window) {
return GTK_BOX (box); return GTK_BOX (box);
} }
static void
show_main_view (GtkWidget *main_view,
gpointer user_data) {
ControlsAdwaita *controls = (ControlsAdwaita *) user_data;
GtkWidget *previous = GTK_WIDGET (controls->previous);
AdwHeaderBar *header = controls->header;
adw_header_bar_remove (header, previous);
}
static void
hide_main_view (GtkWidget *main_view,
gpointer user_data) {
ControlsAdwaita *controls = (ControlsAdwaita *) user_data;
GtkWidget *previous = GTK_WIDGET (controls->previous);
AdwHeaderBar *header = controls->header;
adw_header_bar_pack_start (header, previous);
}
static AdwHeaderBar * static AdwHeaderBar *
create_headerbar (GtkBox *box, AdwLeaflet *views_leaflet, GtkButton **out_previous) { create_headerbar (GtkBox *box, AdwLeaflet *views_leaflet, GtkButton **out_previous) {