<commit_before>/*
 * Copyright (c) 2016 <NAME>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "main_window.h"
#include "../layers/progress_layer.h"
#include "../lib/colors.h"
#include "../lib/util.h"
#include "../lib/data.h"
#include "../lib/strings.h"

#include <pebble.h>
#include <inttypes.h>


static Window *s_main_window;
static MenuLayer *s_menu_layer;
static TextLayer *s_text_layer;
static ProgressLayer *s_progress_layer;


static void progress_layer_hidden() {
  // exit if the window is already unloaded
  if (s_main_window == NULL) {
    return;
  }

  window_set_background_color(s_main_window, COLOR_WINDOW_BACKGROUND);

  Layer *menu_layer = menu_layer_get_layer(s_menu_layer);
  Layer *text_layer = text_layer_get_layer(s_text_layer);

  if (data_get_menu_category_count() != 0) {
    menu_layer_reload_data(s_menu_layer);
    layer_set_hidden(text_layer, true);
    layer_set_hidden(menu_layer, false);
    util_layer_animation_slide_up(menu_layer);
  } else {
    layer_set_hidden(menu_layer, true);
    layer_set_hidden(text_layer, false);
    util_layer_animation_slide_up(text_layer);
  }
}

static void menu_available() {
  progress_layer_hide(s_progress_layer, true, &progress_layer_hidden);
}


static uint16_t menu_get_num_sections_callback(MenuLayer *menu_layer, void *data) {
  return data_get_menu_category_count();
}

static uint16_t menu_get_num_rows_callback(MenuLayer *menu_layer, uint16_t section_index, void *data) {
  if (data_get_menu_category_count() == 0) {
    return 0;
  }
  data_menu_category *category = data_get_menu_category(section_index);
  return category->meal_count;
}

static int16_t menu_get_cell_height(MenuLayer *menu_layer, MenuIndex *cell_index, void *data) {
  data_menu_category *category = data_get_menu_category(cell_index->section);
  data_menu_meal *meal = data_get_menu_meal(category->meal_index + cell_index->row);

  GRect bounds = layer_get_bounds(menu_layer_get_layer(menu_layer));
  GSize size = graphics_text_layout_get_content_size(meal->title, fonts_get_system_font(FONT_KEY_GOTHIC_18),
                                                     GRect(0, 0, bounds.size.w - PBL_IF_RECT_ELSE(9, 45), 64),
                                                     GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft);

  return size.h + 25;
}

static int16_t menu_get_header_height(MenuLayer *menu_layer, uint16_t section_index, void *data) {
  return 20;
}

static void menu_draw_row_callback(GContext* ctx, const Layer *cell_layer, MenuIndex *cell_index, void *data) {
  data_menu_category *category = data_get_menu_category(cell_index->section);
  data_menu_meal *meal = data_get_menu_meal(category->meal_index + cell_index->row);

  GRect bounds = layer_get_bounds(cell_layer);
  graphics_draw_text(ctx, meal->title, fonts_get_system_font(FONT_KEY_GOTHIC_18),
                     GRect(PBL_IF_RECT_ELSE(6, 24), 0, bounds.size.w - PBL_IF_RECT_ELSE(9, 45), bounds.size.h),
                     GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, NULL);

  char price[24];
  snprintf(price, sizeof(price), STRING_FORMAT_MEAL_PRICE, meal->price_student, meal->price_guest);
  graphics_draw_text(ctx, price, fonts_get_system_font(FONT_KEY_GOTHIC_14),
                     GRect(PBL_IF_RECT_ELSE(6, 24), bounds.size.h - 21, bounds.size.w - PBL_IF_RECT_ELSE(9, 45), 16),
                     GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, NULL);
}

static void menu_draw_header_callback(GContext* ctx, const Layer *cell_layer, uint16_t section_index, void *data) {
  data_menu_category *category = data_get_menu_category(section_index);

  GRect bounds = layer_get_bounds(cell_layer);
  graphics_draw_text(ctx, category->title, fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD),
                     GRect(PBL_IF_RECT_ELSE(6, 24), 2, bounds.size.w - PBL_IF_RECT_ELSE(9, 45), 16),
                     GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, NULL);
}


static void window_load(Window *window) {
  window_set_background_color(window, COLOR_WINDOW_PROGRESS_BACKGROUND);

  Layer *window_layer = window_get_root_layer(window);
  GRect bounds = layer_get_bounds(window_layer);<commit_msg>Fix bug of overlapping text if menu name exceeds maximum number of lines<commit_after>/*
 * Copyright (c) 2016 <NAME>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "main_window.h"
#include "../layers/progress_layer.h"
#include "../lib/colors.h"
#include "../lib/util.h"
#include "../lib/data.h"
#include "../lib/strings.h"

#include <pebble.h>
#include <inttypes.h>


static Window *s_main_window;
static MenuLayer *s_menu_layer;
static TextLayer *s_text_layer;
static ProgressLayer *s_progress_layer;


static void progress_layer_hidden() {
  // exit if the window is already unloaded
  if (s_main_window == NULL) {
    return;
  }

  window_set_background_color(s_main_window, COLOR_WINDOW_BACKGROUND);

  Layer *menu_layer = menu_layer_get_layer(s_menu_layer);
  Layer *text_layer = text_layer_get_layer(s_text_layer);

  if (data_get_menu_category_count() != 0) {
    menu_layer_reload_data(s_menu_layer);
    layer_set_hidden(text_layer, true);
    layer_set_hidden(menu_layer, false);
    util_layer_animation_slide_up(menu_layer);
  } else {
    layer_set_hidden(menu_layer, true);
    layer_set_hidden(text_layer, false);
    util_layer_animation_slide_up(text_layer);
  }
}

static void menu_available() {
  progress_layer_hide(s_progress_layer, true, &progress_layer_hidden);
}


static uint16_t menu_get_num_sections_callback(MenuLayer *menu_layer, void *data) {
  return data_get_menu_category_count();
}

static uint16_t menu_get_num_rows_callback(MenuLayer *menu_layer, uint16_t section_index, void *data) {
  if (data_get_menu_category_count() == 0) {
    return 0;
  }
  data_menu_category *category = data_get_menu_category(section_index);
  return category->meal_count;
}

static int16_t menu_get_cell_height(MenuLayer *menu_layer, MenuIndex *cell_index, void *data) {
  data_menu_category *category = data_get_menu_category(cell_index->section);
  data_menu_meal *meal = data_get_menu_meal(category->meal_index + cell_index->row);

  GRect bounds = layer_get_bounds(menu_layer_get_layer(menu_layer));
  GSize size = graphics_text_layout_get_content_size(meal->title, fonts_get_system_font(FONT_KEY_GOTHIC_18),
                                                     GRect(0, 0, bounds.size.w - PBL_IF_RECT_ELSE(9, 45), 64),
                                                     GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft);

  return size.h + 25;
}

static int16_t menu_get_header_height(MenuLayer *menu_layer, uint16_t section_index, void *data) {
  return 20;
}

static void menu_draw_row_callback(GContext* ctx, const Layer *cell_layer, MenuIndex *cell_index, void *data) {
  data_menu_category *category = data_get_menu_category(cell_index->section);
  data_menu_meal *meal = data_get_menu_meal(category->meal_index + cell_index->row);

  GRect bounds = layer_get_bounds(cell_layer);
  graphics_draw_text(ctx, meal->title, fonts_get_system_font(FONT_KEY_GOTHIC_18),
                     GRect(PBL_IF_RECT_ELSE(6, 24), 0, bounds.size.w - PBL_IF_RECT_ELSE(9, 45), bounds.size.h - 25),
                     GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, NULL);

  char price[24];
  snprintf(price, sizeof(price), STRING_FORMAT_MEAL_PRICE, meal->price_student, meal->price_guest);
  graphics_draw_text(ctx, price, fonts_get_system_font(FONT_KEY_GOTHIC_14),
                     GRect(PBL_IF_RECT_ELSE(6, 24), bounds.size.h - 21, bounds.size.w - PBL_IF_RECT_ELSE(9, 45), 16),
                     GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, NULL);
}

static void menu_draw_header_callback(GContext* ctx, const Layer *cell_layer, uint16_t section_index, void *data) {
  data_menu_category *category = data_get_menu_category(section_index);

  GRect bounds = layer_get_bounds(cell_layer);
  graphics_draw_text(ctx, category->title, fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD),
                     GRect(PBL_IF_RECT_ELSE(6, 24), 2, bounds.size.w - PBL_IF_RECT_ELSE(9, 45), 16),
                     GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, NULL);
}


static void window_load(Window *window) {
  window_set_background_color(window, COLOR_WINDOW_PROGRESS_BACKGROUND);

  Layer *window_layer = window_get_root_layer(window);
  GRect bounds = layer_get_bounds(window_layer);
{'fraction_non_alphanumeric': 0.09899290455481803, 'fraction_numerical': 0.01670862897688258, 'mean_word_length': 4.601923076923077, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3280307', 'n_tokens_mistral': 3163, 'n_tokens_neox': 2936, 'n_words': 564}