<commit_before>/**
 * layout.c
 *
 * Copyright (c) 2019 endaaman
 *
 * This software may be modified and distributed under the terms
 * of the MIT license. See the LICENSE file for details.
 */

#include "tym.h"


typedef void (*VteSetColorFunc)(VteTerminal*, const GdkRGBA*);


Layout* layout_init()
{
  Layout* layout = g_malloc0(sizeof(Layout));
  return layout;
}

void layout_close(Layout* layout)
{
  g_free(layout);
}

void layout_build(Layout* layout, GApplication* app, Config* config)
{
  GtkWindow* window = layout->window = GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(app)));
  VteTerminal* vte = layout->vte = VTE_TERMINAL(vte_terminal_new());
  GtkBox* hbox = layout->hbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
  GtkBox* vbox = layout->vbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0));

  int width = config_get_int(config, "width");
  int height = config_get_int(config, "height");
  vte_terminal_set_size(vte, width, height);

  gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(vte));
  gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(hbox));
  gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(vbox));
  gtk_container_set_border_width(GTK_CONTAINER(window), 0);
}

static void layout_apply_color(
  Layout* layout,
  Config* config,
  VteSetColorFunc vte_set_color_func,
  const char* key
) {
  GdkRGBA color;
  bool has_color = config_acquire_color(config, key, &color);
  if (has_color) {
    vte_set_color_func(layout->vte, &color);
  }
}

static void layout_apply_colors(Layout* layout, Config* config)
{
  GdkRGBA* palette = g_new0(GdkRGBA, 16);
  char key[10];
  for (unsigned i = 0; i < 16; i++) {
    // read color_0 .. color_15
    g_snprintf(key, 10, "color_%d", i);
    bool has_color = config_acquire_color(config, key, &palette[i]);
    if (has_color) {
      continue;
    }
    // calc default color
    palette[i].blue  = (((i & 5) ? 0xc000 : 0) + (i > 7 ? 0x3fff : 0)) / 65535.0;
    palette[i].green = (((i & 2) ? 0xc000 : 0) + (i > 7 ? 0x3fff : 0)) / 65535.0;
    palette[i].red   = (((i & 1) ? 0xc000 : 0) + (i > 7 ? 0x3fff : 0)) / 65535.0;
    palette[i].alpha = 0;
  }
  vte_terminal_set_colors(layout->vte, NULL, NULL, palette, 16);
}

void layout_apply_theme(Layout* layout, Config* config)
{
  layout_apply_colors(layout, config);
  layout_apply_color(layout, config, vte_terminal_set_color_bold, "color_bold");
  layout_apply_color(layout, config, vte_terminal_set_color_background, "color_background");
  layout_apply_color(layout, config, vte_terminal_set_color_foreground, "color_foreground");
  layout_apply_color(layout, config, vte_terminal_set_color_cursor, "color_cursor");
  layout_apply_color(layout, config, vte_terminal_set_color_highlight, "color_highlight");
  layout_apply_color(layout, config, vte_terminal_set_color_highlight_foreground, "color_highlight_foreground");
#ifdef TYM_USE_VTE_COLOR_CURSOR_FOREGROUND
  layout_apply_color(layout, config, vte_terminal_set_color_cursor_foreground, "color_cursor_foreground");
#endif
}

void layout_apply_config(Layout* layout, Config* config)
{
  GtkWindow* window = layout->window;
  VteTerminal* vte = layout->vte;
  GtkBox* hbox = layout->hbox;
  GtkBox* vbox = layout->vbox;

  gtk_window_set_title(window, config_get_str(config, "title"));
  char* role = config_has_str(config, "role")
    ? config_get_str(config, "role")
    : NULL;
  gtk_window_set_role(window, role);
  gtk_window_set_icon_name(window, config_get_str(config, "icon"));

  int hpad = config_get_int(config, "padding_horizontal");
  int vpad = config_get_int(config, "padding_vertical");
  gtk_box_set_child_packing(hbox, GTK_WIDGET(vte), true, true, hpad, GTK_PACK_START);
  gtk_box_set_child_packing(vbox, GTK_WIDGET(hbox), true, true, vpad, GTK_PACK_START);

  if (config_has_str(config, "color_window_background")) {
    GtkCssProvider* css_provider = gtk_css_provider_new();
   // TODO: escape
    char* css = g_strdup_printf("window { background-color: %s; }", config_get_str(config, "color_window_background"));
    GError* error = NULL;
    gtk_css_provider_load_from_data(css_provider, css, -1, &error);
    g_free(css);
    if (error) {
      g_warning("Error when parsing css: %s", error->message);
      g_error_free(error);
    } else {
      GtkStyleContext* style_context = gtk_widget_get_style_context(GTK_WIDGET(window));
      gtk_style_context_add_provider(style_context, GTK_STYLE_PROVIDER(css_provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
    }
  }

  vte_terminal_set_cursor_shape(vte, config_get_cursor_shape(config));<commit_msg>use re-parsed value in css<commit_after>/**
 * layout.c
 *
 * Copyright (c) 2019 endaaman
 *
 * This software may be modified and distributed under the terms
 * of the MIT license. See the LICENSE file for details.
 */

#include "tym.h"


typedef void (*VteSetColorFunc)(VteTerminal*, const GdkRGBA*);


Layout* layout_init()
{
  Layout* layout = g_malloc0(sizeof(Layout));
  return layout;
}

void layout_close(Layout* layout)
{
  g_free(layout);
}

void layout_build(Layout* layout, GApplication* app, Config* config)
{
  GtkWindow* window = layout->window = GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(app)));
  VteTerminal* vte = layout->vte = VTE_TERMINAL(vte_terminal_new());
  GtkBox* hbox = layout->hbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
  GtkBox* vbox = layout->vbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0));

  int width = config_get_int(config, "width");
  int height = config_get_int(config, "height");
  vte_terminal_set_size(vte, width, height);

  gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(vte));
  gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(hbox));
  gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(vbox));
  gtk_container_set_border_width(GTK_CONTAINER(window), 0);
}

static void layout_apply_color(
  Layout* layout,
  Config* config,
  VteSetColorFunc vte_set_color_func,
  const char* key
) {
  GdkRGBA color;
  bool has_color = config_acquire_color(config, key, &color);
  if (has_color) {
    vte_set_color_func(layout->vte, &color);
  }
}

static void layout_apply_colors(Layout* layout, Config* config)
{
  GdkRGBA* palette = g_new0(GdkRGBA, 16);
  char key[10];
  for (unsigned i = 0; i < 16; i++) {
    // read color_0 .. color_15
    g_snprintf(key, 10, "color_%d", i);
    bool has_color = config_acquire_color(config, key, &palette[i]);
    if (has_color) {
      continue;
    }
    // calc default color
    palette[i].blue  = (((i & 5) ? 0xc000 : 0) + (i > 7 ? 0x3fff : 0)) / 65535.0;
    palette[i].green = (((i & 2) ? 0xc000 : 0) + (i > 7 ? 0x3fff : 0)) / 65535.0;
    palette[i].red   = (((i & 1) ? 0xc000 : 0) + (i > 7 ? 0x3fff : 0)) / 65535.0;
    palette[i].alpha = 0;
  }
  vte_terminal_set_colors(layout->vte, NULL, NULL, palette, 16);
}

void layout_apply_theme(Layout* layout, Config* config)
{
  layout_apply_colors(layout, config);
  layout_apply_color(layout, config, vte_terminal_set_color_bold, "color_bold");
  layout_apply_color(layout, config, vte_terminal_set_color_background, "color_background");
  layout_apply_color(layout, config, vte_terminal_set_color_foreground, "color_foreground");
  layout_apply_color(layout, config, vte_terminal_set_color_cursor, "color_cursor");
  layout_apply_color(layout, config, vte_terminal_set_color_highlight, "color_highlight");
  layout_apply_color(layout, config, vte_terminal_set_color_highlight_foreground, "color_highlight_foreground");
#ifdef TYM_USE_VTE_COLOR_CURSOR_FOREGROUND
  layout_apply_color(layout, config, vte_terminal_set_color_cursor_foreground, "color_cursor_foreground");
#endif
}

void layout_apply_config(Layout* layout, Config* config)
{
  GtkWindow* window = layout->window;
  VteTerminal* vte = layout->vte;
  GtkBox* hbox = layout->hbox;
  GtkBox* vbox = layout->vbox;

  gtk_window_set_title(window, config_get_str(config, "title"));
  char* role = config_has_str(config, "role")
    ? config_get_str(config, "role")
    : NULL;
  gtk_window_set_role(window, role);
  gtk_window_set_icon_name(window, config_get_str(config, "icon"));

  int hpad = config_get_int(config, "padding_horizontal");
  int vpad = config_get_int(config, "padding_vertical");
  gtk_box_set_child_packing(hbox, GTK_WIDGET(vte), true, true, hpad, GTK_PACK_START);
  gtk_box_set_child_packing(vbox, GTK_WIDGET(hbox), true, true, vpad, GTK_PACK_START);

  if (config_has_str(config, "color_window_background")) {
    GdkRGBA color;
    if (config_acquire_color(config, "color_window_background", &color)) {
      char* color_str = gdk_rgba_to_string(&color);
      char* css = g_strdup_printf("window { background-color: %s; }", color_str);
      g_free(color_str);
      GtkCssProvider* css_provider = gtk_css_provider_new();
      GError* error = NULL;
      gtk_css_provider_load_from_data(css_provider, css, -1, &error);
      g_free(css);
      if (error) {
        g_warning("Error when parsing css: %s", error->message);
        g_error_free(error);
      } else {
        GtkStyleContext* style_context = gtk_widget_get_style_context(GTK_WIDGET(window));
        gtk_style_context_add_provider(style_context, GTK_STYLE_PROVIDER(css_provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
      }
    }
  }

  vte_terminal_set_cursor_shape(vte, config_get_cursor_shape(config));
{'fraction_non_alphanumeric': 0.12501344809037115, 'fraction_numerical': 0.0157073695535234, 'mean_word_length': 5.446601941747573, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7267269', 'n_tokens_mistral': 3661, 'n_tokens_neox': 3459, 'n_words': 642}