A new style desktop shell for Windows 11: menu bar, Dock, Finder, Spotlight, Control Center and Liquid Glass, in native C++.
Rust
//! Calendário (src\calendario\main.cpp): Dia, Semana, Mês e Ano, calendários na lateral, feriados (Nager.Date),
//! lembretes com data, importação de .ics. Dados em %APPDATA%\Shell\calendario.txt.
//! No modo --foto não baixa feriados (nem grava feriados.txt): só usa o que já estiver baixado.
#![windows_subsystem = "windows"]
use std::collections::BTreeSet;
use comum::app2d::{self, *};
use comum::windows::core::*;
use comum::windows::Win32::Foundation::*;
use comum::windows::Win32::Globalization::*;
use comum::windows::Win32::Graphics::DirectWrite::*;
use comum::windows::Win32::Graphics::Gdi::HDC;
use comum::windows::Win32::System::Diagnostics::Debug::MessageBeep;
use comum::windows::Win32::System::Time::SystemTimeToTzSpecificLocalTime;
use comum::windows::Win32::UI::Input::KeyboardAndMouse::*;
use comum::windows::Win32::UI::WindowsAndMessaging::*;
use comum::*;
const WEEKHEAD: f32 = 30.0;
const HOUR: f32 = 48.0;
const ALLDAY: f32 = 26.0;
const LEFTCOL: f32 = 56.0;
const WM_FIELDLOST: u32 = WM_APP + 77;
const WM_HOLIDAYS: u32 = WM_APP + 6;
const HOL_CAL: i32 = -5;
const COLOR_NAMES: [&str; 7] = ["Azul", "Vermelho", "Laranja", "Verde", "Roxo", "Amarelo", "Marrom"];
const V_DAY: i32 = 0;
const V_WEEK: i32 = 1;
const V_MONTH: i32 = 2;
const V_YEAR: i32 = 3;
const DIA: i64 = 24 * 60;
#[derive(Clone, Default)]
struct Ev {
id: i32,
start: i64,
end: i64,
all_day: bool,
title: String,
color: i32,
notes: String,
cal: i32,
}
#[derive(Clone)]
struct Cal {
id: i32,
name: String,
color: i32,
on: bool,
}
struct RemItem {
due: i64,
title: String,
done: bool,
}
struct HolDay {
y: i32,
m: i32,
d: i32,
name: String,
}
#[derive(Clone, Copy)]
struct Hit {
kind: i32,
ev: i32,
day: i64,
r: RectF,
}
#[derive(Default)]
struct App {
tb: f32,
side_w: f32,
ev: Vec<Ev>,
cals: Vec<Cal>,
side: bool,
search: String,
next_id: i32,
file: std::path::PathBuf,
rem_file: std::path::PathBuf,
view: i32,
anchor: i64,
sel_day: i64,
sel_ev: i32,
scroll_h: f32,
editing: i32,
hol_on: bool,
hol_cc: String,
hol_years: BTreeSet<i32>,
hol_next_id: i32,
first_dow: i32,
first_dow_set: bool,
hits: Vec<Hit>,
tf: std::collections::HashMap<&'static str, IDWriteTextFormat>,
}
static A: Global<Option<App>> = Global::new(None);
fn a() -> &'static mut App {
A.get().as_mut().unwrap()
}
fn tf(n: &str) -> &'static IDWriteTextFormat {
&a().tf[n]
}
fn hit(kind: i32, ev: i32, day: i64, r: RectF) {
a().hits.push(Hit { kind, ev, day, r });
}
fn ev_color(i: i32) -> Cor {
let c = [rgb(10, 132, 255, 1.0), rgb(255, 69, 58, 1.0), rgb(255, 159, 10, 1.0), rgb(48, 209, 88, 1.0), rgb(191, 90, 242, 1.0), rgb(255, 214, 10, 1.0), rgb(172, 142, 104, 1.0)];
c[i.rem_euclid(7) as usize]
}
fn find_cal(id: i32) -> Option<&'static mut Cal> {
a().cals.iter_mut().find(|c| c.id == id)
}
fn cal_on(e: &Ev) -> bool {
find_cal(e.cal).map(|c| c.on).unwrap_or(true)
}
fn find_ev(id: i32) -> Option<&'static mut Ev> {
a().ev.iter_mut().find(|e| e.id == id)
}
fn save() {
if let Some(hc) = find_cal(HOL_CAL) {
a().hol_on = hc.on;
}
let a = a();
let mut out = vec!["# Calendário do WacOS (E = evento: id, início, fim em minutos desde 1970, dia inteiro, título, cor, notas)".to_string()];
out.push(format!("S\tsemana\t{}", if a.first_dow_set { a.first_dow } else { -1 }));
out.push(format!("S\tferiados\t{}", if a.hol_on { 1 } else { 0 }));
for c in a.cals.iter().filter(|c| c.id != HOL_CAL) {
out.push(format!("C\t{}\t{}\t{}\t{}", c.id, esc(&c.name), c.color, if c.on { 1 } else { 0 }));
}
for e in a.ev.iter().filter(|e| e.id >= 0) {
out.push(format!("E\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}", e.id, e.start, e.end, if e.all_day { 1 } else { 0 }, esc(&e.title), e.color, esc(&e.notes), e.cal));
}
write_lines(&a.file, &out);
}
fn load() {
let a = a();
a.ev.clear();
a.cals.clear();
for line in read_lines(&a.file) {
let f = split(&line);
let ni = |i: usize| f[i].parse::<i32>().unwrap_or(0);
let nl = |i: usize| f[i].parse::<i64>().unwrap_or(0);
if f.len() >= 7 && f[0] == "E" {
let mut e = Ev { id: ni(1), start: nl(2), end: nl(3), all_day: f[4] == "1", title: unesc(&f[5]), color: ni(6), notes: if f.len() > 7 { unesc(&f[7]) } else { String::new() }, cal: 1 };
if f.len() > 8 {
e.cal = ni(8);
}
a.ev.push(e);
} else if f.len() >= 5 && f[0] == "C" {
a.cals.push(Cal { id: ni(1), name: unesc(&f[2]), color: ni(3), on: f[4] == "1" });
} else if f.len() >= 3 && f[0] == "S" && f[1] == "semana" {
let v = ni(2);
if v == 0 || v == 1 {
a.first_dow = v;
a.first_dow_set = true;
}
} else if f.len() >= 3 && f[0] == "S" && f[1] == "feriados" {
a.hol_on = f[2] != "0";
}
}
if a.cals.is_empty() {
a.cals = vec![
Cal { id: 1, name: tr("Pessoal"), color: 0, on: true },
Cal { id: 2, name: tr("Trabalho"), color: 2, on: true },
Cal { id: 3, name: tr("Família"), color: 3, on: true },
];
}
unsafe {
if !a.first_dow_set {
let mut b = [0u16; 4];
if GetLocaleInfoEx(PCWSTR::null() /* LOCALE_NAME_USER_DEFAULT */, LOCALE_IFIRSTDAYOFWEEK, Some(&mut b)) > 0 {
let v: i32 = from_w(&b).parse().unwrap_or(0);
a.first_dow = if (v + 1) % 7 == 1 { 1 } else { 0 };
}
}
let mut cc = [0u16; 8];
if GetUserDefaultGeoName(&mut cc) > 0 {
let s = from_w(&cc);
if s.chars().count() == 2 {
a.hol_cc = s;
}
}
}
if !a.hol_cc.is_empty() {
a.cals.push(Cal { id: HOL_CAL, name: tr("Feriados"), color: 1, on: a.hol_on });
}
for e in &a.ev {
a.next_id = a.next_id.max(e.id + 1);
}
for c in &a.cals {
a.next_id = a.next_id.max(c.id + 1);
}
}
fn reminders() -> Vec<RemItem> {
read_lines(&a().rem_file)
.iter()
.filter_map(|l| {
let f = split(l);
let due = f.get(4).and_then(|v| v.parse::<i64>().ok()).unwrap_or(0);
(f.len() >= 8 && f[0] == "R" && due != 0).then(|| RemItem { due, title: unesc(&f[5]), done: f[3] == "1" })
})
.collect()
}
fn add_days(day: i64, n: i32) -> i64 {
let mut s = from_min(day);
s.wHour = 12;
day_start(to_min(&s) + n as i64 * DIA)
}
fn month_start(m: i64) -> i64 {
let mut s = from_min(m);
s.wDay = 1;
s.wHour = 0;
s.wMinute = 0;
to_min(&s)
}
fn add_months(m: i64, n: i32) -> i64 {
let mut s = from_min(month_start(m));
let mo = s.wMonth as i32 - 1 + n;
s.wYear = (s.wYear as i32 + mo.div_euclid(12)) as u16;
s.wMonth = (mo.rem_euclid(12) + 1) as u16;
to_min(&s)
}
fn week_start(m: i64) -> i64 {
let d = day_start(m);
add_days(d, -((from_min(d).wDayOfWeek as i32 - a().first_dow + 7) % 7))
}
fn dow_at(col: i32) -> i32 {
(col + a().first_dow) % 7
}
fn on_day(e: &Ev, day: i64) -> bool {
let next = add_days(day, 1);
e.start < next && (e.end > day || (e.end <= e.start && e.start >= day))
}
fn hm(m: i64) -> String {
let s = from_min(m);
format!("{}:{:02}", s.wHour, s.wMinute)
}
fn mes_cap(m: i32) -> String {
let mn = month_name(m);
if idioma::en() {
mn
} else {
idioma::cap(&mn)
}
}
// ---- feriados
fn add_holiday(hd: &HolDay) {
let st = SYSTEMTIME { wYear: hd.y as u16, wMonth: hd.m as u16, wDay: hd.d as u16, ..Default::default() };
let day = to_min(&st);
if a().ev.iter().any(|e| e.cal == HOL_CAL && e.start == day && e.title == hd.name) {
return;
}
let id = a().hol_next_id;
a().hol_next_id -= 1;
a().ev.push(Ev { id, start: day, end: add_days(day, 1), all_day: true, title: hd.name.clone(), color: 1, notes: String::new(), cal: HOL_CAL });
}
fn hol_file() -> std::path::PathBuf {
data_path("feriados.txt")
}
fn load_holiday_cache() {
for line in read_lines(&hol_file()) {
let f = split(&line);
if f.len() < 5 || f[0] != "H" || f[1] != a().hol_cc {
continue;
}
let y: i32 = f[2].parse().unwrap_or(0);
let v: i32 = f[3].parse().unwrap_or(0);
a().hol_years.insert(y);
if v > 0 {
add_holiday(&HolDay { y: v / 10000, m: v / 100 % 100, d: v % 100, name: unesc(&f[4]) });
}
}
}
/// o próximo "key":"valor" a partir de p (com \uXXXX)
fn json_str(j: &str, p: &mut usize, key: &str) -> String {
let k = format!("\"{key}\":\"");
let Some(a_) = j[*p..].find(&k) else { return String::new() };
let ini = *p + a_ + k.len();
let b = j.as_bytes();
let mut out = String::new();
let mut i = ini;
while i < b.len() && b[i] != b'"' {
if b[i] == b'\\' && i + 1 < b.len() {
i += 1;
if b[i] == b'u' && i + 4 < b.len() {
if let Some(c) = u32::from_str_radix(&j[i + 1..i + 5], 16).ok().and_then(char::from_u32) {
out.push(c);
}
i += 5;
continue;
}
out.push(if b[i] == b'n' { ' ' } else { b[i] as char });
i += 1;
continue;
}
let c = j[i..].chars().next().unwrap();
out.push(c);
i += c.len_utf8();
}
*p = i;
out
}
fn request_holidays(year: i32) {
if modo_foto() || a().hol_cc.is_empty() || !a().hol_on || year < 1970 || a().hol_years.contains(&year) {
return;
}
a().hol_years.insert(year);
let cc = a().hol_cc.clone();
let h = wnd().0 as usize;
std::thread::spawn(move || {
let body = rede::get("date.nager.at", &format!("/api/v3/PublicHolidays/{year}/{cc}"), "WacOS-Calendar/1.0", 256 * 1024);
let ok = body.as_ref().map(|b| !b.is_empty()).unwrap_or(false);
let body = String::from_utf8_lossy(&body.unwrap_or_default()).into_owned();
let mut list: Vec<HolDay> = Vec::new();
let mut p = 0;
loop {
let d = json_str(&body, &mut p, "date");
if d.len() < 10 {
break;
}
let n = json_str(&body, &mut p, "localName");
list.push(HolDay { y: d[0..4].parse().unwrap_or(0), m: d[5..7].parse().unwrap_or(0), d: d[8..10].parse().unwrap_or(0), name: n });
}
let ptr = Box::into_raw(Box::new(list));
unsafe {
if PostMessageW(HWND(h as *mut _), WM_HOLIDAYS, WPARAM(if ok { year } else { -year } as isize as usize), LPARAM(ptr as isize)).is_err() {
drop(Box::from_raw(ptr));
}
}
});
}
fn holidays_arrived(year: i32, list: Box<Vec<HolDay>>) {
if year < 0 {
a().hol_years.remove(&-year);
return;
}
let mut lines = read_lines(&hol_file());
if lines.is_empty() {
lines.push("# Feriados baixados pelo Calendário do WacOS (Nager.Date): H, país, ano, aaaammdd, nome".into());
}
let cc = a().hol_cc.clone();
lines.push(format!("H\t{cc}\t{year}\t0\t"));
for hd in list.iter() {
lines.push(format!("H\t{cc}\t{year}\t{:04}{:02}{:02}\t{}", hd.y, hd.m, hd.d, esc(&hd.name)));
add_holiday(hd);
}
write_lines(&hol_file(), &lines);
}
// ---- desenho
fn toolbar(sz: (f32, f32), dark: bool, text_c: Cor, gray: Cor) {
let an = from_min(a().anchor);
let mon = mes_cap(an.wMonth as i32);
let mw = text_w(tf("month"), &mon);
text(&mon, tf("month"), rf(20.0, 12.0, 20.0 + mw + 4.0, 52.0), text_c);
text(&an.wYear.to_string(), tf("year"), rf(26.0 + mw, 12.0, 26.0 + mw + 120.0, 52.0), gray);
let names = ["Dia", "Semana", "Mês", "Ano"];
let seg_w = 70.0;
let sx = sz.0 / 2.0 - seg_w * 2.0;
fill(rf(sx, 18.0, sx + seg_w * 4.0, 44.0), if dark { rgb(58, 58, 60, 1.0) } else { rgb(232, 232, 237, 1.0) }, 7.0);
for i in 0..4 {
let r = rf(sx + i as f32 * seg_w + 2.0, 20.0, sx + (i + 1) as f32 * seg_w - 2.0, 42.0);
if a().view == i {
fill(r, if dark { rgb(99, 99, 102, 1.0) } else { rgb(255, 255, 255, 1.0) }, 6.0);
}
text(&tr(names[i as usize]), tf("btn"), r, text_c);
hit(4, i, 0, r);
}
let prev = rf(sz.0 - 150.0, 18.0, sz.0 - 122.0, 44.0);
let today = rf(sz.0 - 120.0, 18.0, sz.0 - 50.0, 44.0);
let next = rf(sz.0 - 48.0, 18.0, sz.0 - 20.0, 44.0);
fill(today, if dark { rgb(58, 58, 60, 1.0) } else { rgb(232, 232, 237, 1.0) }, 7.0);
text(&tr("Hoje"), tf("btn"), today, text_c);
text("‹", tf("month"), prev, text_c);
text("›", tf("month"), next, text_c);
hit(3, -1, 0, prev);
hit(3, 0, 0, today);
hit(3, 1, 0, next);
}
fn ev_pill(e: &Ev, day: i64, er: RectF, dark: bool, text_c: Cor, com_hora: bool) {
let c = ev_color(e.color);
let sel = e.id == a().sel_ev;
if e.all_day || sel {
fill(er, if sel { c } else { col(c.r, c.g, c.b, if dark { 0.35 } else { 0.22 }) }, 4.0);
} else {
circle(pt(er.left + 6.0, (er.top + er.bottom) / 2.0), 3.5, c, true, 1.5);
}
let t = if e.all_day || !com_hora { e.title.clone() } else { format!("{} {}", hm(e.start), e.title) };
if e.id != a().editing {
text(&t, tf("ev"), rf(er.left + if e.all_day || sel { 6.0 } else { 14.0 }, er.top, er.right - 2.0, er.bottom), if sel { rgb(255, 255, 255, 1.0) } else { text_c });
}
hit(1, e.id, day, er);
}
fn paint_month(sz: (f32, f32), dark: bool, text_c: Cor, gray: Cor, rems: &[RemItem]) {
let tb = a().tb;
let ms = month_start(a().anchor);
let first = week_start(ms);
let am = from_min(ms);
let top = tb + WEEKHEAD;
let cw = sz.0 / 7.0;
let ch = (sz.1 - top) / 6.0;
for d in 0..7 {
text(&weekday_short(dow_at(d)), tf("headday"), rf(d as f32 * cw, tb, (d + 1) as f32 * cw - 8.0, tb + WEEKHEAD), gray);
}
hit(13, 0, 0, rf(0.0, tb, 7.0 * cw, tb + WEEKHEAD));
let today = day_start(now_min());
let linha = if dark { rgb(255, 255, 255, 0.10) } else { rgb(0, 0, 0, 0.10) };
for i in 0..42 {
let day = add_days(first, i);
let s = from_min(day);
let (cx, cy) = ((i % 7) as f32, (i / 7) as f32);
let c = rf(cx * cw, top + cy * ch, (cx + 1.0) * cw, top + (cy + 1.0) * ch);
let other = s.wMonth != am.wMonth;
if s.wDayOfWeek == 0 || s.wDayOfWeek == 6 {
fill(c, if dark { rgb(255, 255, 255, 0.025) } else { rgb(0, 0, 0, 0.025) }, 0.0);
}
if day == a().sel_day {
fill(c, if dark { rgb(10, 132, 255, 0.16) } else { rgb(10, 132, 255, 0.08) }, 0.0);
}
fill(rf(c.left, c.top, c.right, c.top + 1.0), linha, 0.0);
fill(rf(c.right - 1.0, c.top, c.right, c.bottom), linha, 0.0);
let mut n = s.wDay.to_string();
if s.wDay == 1 {
let mn = month_name(s.wMonth as i32);
n = format!("{n} {}", if idioma::pt() || idioma::en() { format!("{}.", mn.chars().take(3).collect::<String>()) } else { idioma::month_name(s.wMonth as i32, true) });
}
if day == today {
let w_ = text_w(tf("daynum"), &n) + 12.0;
fill(rf(c.right - 8.0 - w_, c.top + 4.0, c.right - 8.0, c.top + 26.0), rgb(255, 59, 48, 1.0), 11.0);
text(&n, tf("daynum"), rf(c.right - 8.0 - w_, c.top + 4.0, c.right - 14.0, c.top + 26.0), rgb(255, 255, 255, 1.0));
} else {
text(&n, tf("daynum"), rf(c.right - 70.0, c.top + 4.0, c.right - 8.0, c.top + 26.0), if other { gray } else { text_c });
}
hit(2, -1, day, c);
let mut y = c.top + 30.0;
let line_h = 18.0;
let mut list: Vec<Ev> = a().ev.iter().filter(|e| on_day(e, day) && cal_on(e)).cloned().collect();
list.sort_by(|x, y| if x.all_day != y.all_day { y.all_day.cmp(&x.all_day) } else { x.start.cmp(&y.start) });
let rl: Vec<&RemItem> = rems.iter().filter(|r| day_start(r.due) == day).collect();
let total = list.len() + rl.len();
let capacity = ((c.bottom - y - 4.0) / line_h).max(0.0) as usize;
let lim = capacity.saturating_sub(if total > capacity { 1 } else { 0 });
let mut shown = 0;
for e in &list {
if shown >= lim {
break;
}
ev_pill(e, day, rf(c.left + 4.0, y, c.right - 4.0, y + line_h - 2.0), dark, text_c, true);
y += line_h;
shown += 1;
}
for r in &rl {
if shown >= lim {
break;
}
let er = rf(c.left + 4.0, y, c.right - 4.0, y + line_h - 2.0);
text(&format!("{}{} {}", if r.done { "☑ " } else { "☐ " }, hm(r.due), r.title), tf("ev"), rf(er.left + 4.0, er.top, er.right, er.bottom), gray);
y += line_h;
shown += 1;
}
if total > shown {
text(&cf!(tr("mais %d…"), total - shown), tf("evsmall"), rf(c.left + 8.0, y, c.right - 4.0, y + line_h - 2.0), gray);
}
}
}
fn paint_grid(sz: (f32, f32), dark: bool, text_c: Cor, gray: Cor, rems: &[RemItem]) {
let tb = a().tb;
let cols = if a().view == V_DAY { 1 } else { 7 };
let first = if a().view == V_DAY { day_start(a().anchor) } else { week_start(a().anchor) };
let today = day_start(now_min());
let cw = (sz.0 - LEFTCOL) / cols as f32;
let linha = if dark { rgb(255, 255, 255, 0.10) } else { rgb(0, 0, 0, 0.10) };
for d in 0..cols {
let day = add_days(first, d);
let s = from_min(day);
let h = rf(LEFTCOL + d as f32 * cw, tb, LEFTCOL + (d + 1) as f32 * cw, tb + WEEKHEAD);
let b = format!("{} {}", weekday_short(s.wDayOfWeek as i32), s.wDay);
text(&b, tf("headday"), h, if day == today { rgb(255, 59, 48, 1.0) } else { text_c });
hit(2, -1, day, rf(h.left, tb + WEEKHEAD, h.right, tb + WEEKHEAD + ALLDAY));
if let Some(e) = a().ev.iter().find(|e| e.all_day && on_day(e, day) && cal_on(e)).cloned() {
let er = rf(h.left + 2.0, tb + WEEKHEAD + 3.0, h.right - 2.0, tb + WEEKHEAD + ALLDAY - 3.0);
let c = ev_color(e.color);
fill(er, if e.id == a().sel_ev { c } else { col(c.r, c.g, c.b, 0.3) }, 4.0);
if e.id != a().editing {
text(&e.title, tf("ev"), rf(er.left + 6.0, er.top, er.right, er.bottom), if e.id == a().sel_ev { rgb(255, 255, 255, 1.0) } else { text_c });
}
hit(1, e.id, day, er);
}
}
let top = tb + WEEKHEAD + ALLDAY;
fill(rf(0.0, top - 1.0, sz.0, top), linha, 0.0);
push_clip(rf(0.0, top, sz.0, sz.1));
let y0 = top - a().scroll_h;
for hr in 0..24 {
let y = y0 + hr as f32 * HOUR;
fill(rf(LEFTCOL, y, sz.0, y + 1.0), linha, 0.0);
if hr > 0 {
text(&format!("{hr}:00"), tf("hour"), rf(0.0, y - 9.0, LEFTCOL - 8.0, y + 9.0), gray);
}
}
for d in 0..cols {
let day = add_days(first, d);
let x = LEFTCOL + d as f32 * cw;
fill(rf(x, top, x + 1.0, sz.1), linha, 0.0);
hit(2, -1, day, rf(x, top, x + cw, sz.1));
for e in a().ev.clone().iter() {
if e.all_day || !on_day(e, day) || !cal_on(e) {
continue;
}
let s = e.start.max(day);
let en = e.end.max(e.start + 30).min(add_days(day, 1));
let ya = y0 + (s - day) as f32 / 60.0 * HOUR;
let yb = y0 + (en - day) as f32 / 60.0 * HOUR;
let er = rf(x + 2.0, ya + 1.0, x + cw - 3.0, (ya + 18.0).max(yb - 1.0));
let c = ev_color(e.color);
let sel = e.id == a().sel_ev;
fill(er, if sel { c } else { col(c.r, c.g, c.b, if dark { 0.35 } else { 0.2 }) }, 5.0);
fill(rf(er.left, er.top, er.left + 3.0, er.bottom), c, 1.5);
if e.id != a().editing {
text(&e.title, tf("ev"), rf(er.left + 8.0, er.top + 2.0, er.right - 2.0, er.top + 18.0), if sel { rgb(255, 255, 255, 1.0) } else { text_c });
if er.bottom - er.top > 34.0 {
text(&format!("{} – {}", hm(e.start), hm(e.end)), tf("evsmall"), rf(er.left + 8.0, er.top + 18.0, er.right - 2.0, er.top + 34.0), if sel { rgb(255, 255, 255, 1.0) } else { gray });
}
}
hit(1, e.id, day, er);
}
for r in rems.iter().filter(|r| day_start(r.due) == day) {
let ya = y0 + (r.due - day) as f32 / 60.0 * HOUR;
text(&format!("{}{}", if r.done { "☑ " } else { "☐ " }, r.title), tf("ev"), rf(x + 6.0, ya, x + cw - 4.0, ya + 18.0), gray);
}
if day == today {
let y = y0 + (now_min() - day) as f32 / 60.0 * HOUR;
fill(rf(x, y - 1.0, x + cw, y + 1.0), rgb(255, 59, 48, 1.0), 0.0);
circle(pt(x + 2.0, y), 4.0, rgb(255, 59, 48, 1.0), true, 1.5);
}
}
pop_clip();
}
fn paint_year(sz: (f32, f32), text_c: Cor, gray: Cor) {
let an = from_min(a().anchor);
let top = a().tb + 8.0;
let mw = (sz.0 - 40.0) / 4.0;
let mh = (sz.1 - top - 8.0) / 3.0;
let today = day_start(now_min());
let tn = from_min(today);
for m in 0..12 {
let x0 = 20.0 + (m % 4) as f32 * mw;
let y0 = top + (m / 4) as f32 * mh;
let mn = mes_cap(m + 1);
let cur = an.wYear == tn.wYear && m + 1 == tn.wMonth as i32;
text(&mn, tf("headday"), rf(x0 + 6.0, y0 + 4.0, x0 + mw - 10.0, y0 + 26.0), if cur { rgb(255, 59, 48, 1.0) } else { text_c });
let s1 = SYSTEMTIME { wYear: an.wYear, wMonth: (m + 1) as u16, wDay: 1, ..Default::default() };
let first = to_min(&s1);
let next = add_months(first, 1);
let wd = (from_min(first).wDayOfWeek as i32 - a().first_dow + 7) % 7;
let days = ((day_start(next + 12 * 60) - first + 12 * 60) / DIA) as i32;
let cw = (mw - 20.0) / 7.0;
let ch = ((mh - 50.0) / 6.0).min(cw);
for d in 0..7 {
let l: String = weekday_short(dow_at(d)).chars().take(1).collect();
text(&l, tf("yearday"), rf(x0 + d as f32 * cw, y0 + 26.0, x0 + (d + 1) as f32 * cw, y0 + 42.0), gray);
}
for d in 1..=days {
let idx = wd + d - 1;
let c = rf(x0 + (idx % 7) as f32 * cw, y0 + 44.0 + (idx / 7) as f32 * ch, x0 + (idx % 7 + 1) as f32 * cw, y0 + 44.0 + (idx / 7 + 1) as f32 * ch);
let day = add_days(first, d - 1);
let has = a().ev.iter().any(|e| cal_on(e) && on_day(e, day));
let ctr = pt((c.left + c.right) / 2.0, (c.top + c.bottom) / 2.0);
if day == today {
circle(ctr, cw.min(ch) * 0.42, rgb(255, 59, 48, 1.0), true, 1.5);
}
text(&d.to_string(), tf("yearday"), c, if day == today { rgb(255, 255, 255, 1.0) } else { text_c });
if has && day != today {
circle(pt(ctr.x, c.bottom - 2.0), 1.8, rgb(10, 132, 255, 1.0), true, 1.5);
}
hit(8, -1, day, c);
}
}
}
fn mini_month(x0: f32, bottom: f32, dark: bool, text_c: Cor) {
let soft = if dark { rgb(255, 255, 255, 0.5) } else { rgb(0, 0, 0, 0.45) };
let faint = if dark { rgb(255, 255, 255, 0.28) } else { rgb(0, 0, 0, 0.25) };
fill(rf(x0 + 19.0, bottom - 223.5, x0 + 199.0, bottom - 223.0), if dark { rgb(255, 255, 255, 0.12) } else { rgb(0, 0, 0, 0.10) }, 0.0);
let ms = month_start(if a().sel_day != 0 { a().sel_day } else { a().anchor });
let first = week_start(ms);
let am = from_min(ms);
let ty = bottom - 190.0;
text(&format!("{} {}", mes_cap(am.wMonth as i32), am.wYear), tf("minit"), rf(x0 + 40.0, ty - 10.0, x0 + 180.0, ty + 10.0), soft);
let pv = rf(x0 + 16.0, ty - 10.0, x0 + 36.0, ty + 10.0);
let nx = rf(x0 + 184.5, ty - 10.0, x0 + 204.5, ty + 10.0);
text("‹", tf("month"), rf(pv.left, pv.top - 8.0, pv.right, pv.bottom + 4.0), soft);
text("›", tf("month"), rf(nx.left, nx.top - 8.0, nx.right, nx.bottom + 4.0), soft);
hit(10, -1, 0, pv);
hit(10, 1, 0, nx);
for d in 0..7 {
let cx = x0 + 26.5 + d as f32 * 28.0;
let l: String = weekday_short(dow_at(d)).chars().take(1).collect();
text(&l, tf("miniw"), rf(cx - 12.0, bottom - 167.5, cx + 12.0, bottom - 147.5), soft);
}
let today = day_start(now_min());
for i in 0..42 {
let day = add_days(first, i);
let s = from_min(day);
let cx = x0 + 26.5 + (i % 7) as f32 * 28.0;
let cy = bottom - 133.5 + (i / 7) as f32 * 23.5;
let c = rf(cx - 12.0, cy - 11.0, cx + 12.0, cy + 11.0);
let other = s.wMonth != am.wMonth;
let wk = s.wDayOfWeek == 0 || s.wDayOfWeek == 6;
if day == today {
circle(pt(cx, cy), 9.5, rgb(255, 59, 48, 1.0), true, 1.5);
} else if day == a().sel_day {
circle(pt(cx, cy), 9.5, if dark { rgb(255, 255, 255, 0.14) } else { rgb(0, 0, 0, 0.08) }, true, 1.5);
}
let c_txt = if day == today {
rgb(255, 255, 255, 1.0)
} else if other {
faint
} else if wk {
soft
} else {
text_c
};
text(&s.wDay.to_string(), tf("minid"), c, c_txt);
hit(11, -1, day, c);
}
}
/// o conteúdo (Ano, Mês ou a grade) deslocado em x0
fn paint_content(x0: f32, sz: (f32, f32), dark: bool, text_c: Cor, gray: Cor, rems: &[RemItem]) {
let csz = (sz.0 - x0, sz.1);
unsafe {
app2d::rt().SetTransform(&mat::trans(x0, 0.0));
}
let h0 = a().hits.len();
match a().view {
V_YEAR => paint_year(csz, text_c, gray),
V_MONTH => paint_month(csz, dark, text_c, gray, rems),
_ => paint_grid(csz, dark, text_c, gray, rems),
}
unsafe {
app2d::rt().SetTransform(&mat::ident());
}
for h in &mut a().hits[h0..] {
h.r.left += x0;
h.r.right += x0;
}
}
fn check(box_: RectF) {
unsafe {
br().SetColor(&rgb(255, 255, 255, 1.0));
app2d::rt().DrawLine(pt(box_.left + 3.0, box_.top + 6.8), pt(box_.left + 5.5, box_.top + 9.3), br(), 1.6, None);
app2d::rt().DrawLine(pt(box_.left + 5.5, box_.top + 9.3), pt(box_.left + 10.0, box_.top + 3.8), br(), 1.6, None);
}
}
fn paint_golden() {
let dark = dark();
let sz = size();
let sz2 = (sz.width, sz.height);
let text_c = if dark { rgb(245, 245, 247, 1.0) } else { rgb(29, 29, 31, 1.0) };
let gray = if dark { rgb(152, 152, 157, 1.0) } else { rgb(134, 134, 139, 1.0) };
clear(if dark { rgb(30, 30, 30, 1.0) } else { rgb(255, 255, 255, 1.0) });
let rems = reminders();
let side_w = a().side_w;
let x0 = if a().side { side_w } else { 0.0 };
paint_content(x0, sz2, dark, text_c, gray, &rems);
let tb = a().tb;
fill(rf(x0, 0.0, sz.width, tb), if dark { rgb(30, 30, 30, 1.0) } else { rgb(255, 255, 255, 1.0) }, 0.0);
if a().side {
fill(rf(0.0, 0.0, side_w, sz.height), if dark { rgb(43, 41, 40, 1.0) } else { rgb(237, 237, 236, 1.0) }, 0.0);
fill(rf(side_w - 0.5, 0.0, side_w, sz.height), if dark { rgb(0, 0, 0, 0.5) } else { rgb(0, 0, 0, 0.08) }, 0.0);
let grp = rf(133.5, 8.0, 211.0, 44.0);
glass_button(grp, dark, false);
let b1 = rf(grp.left + 4.0, 12.0, grp.left + 35.0, 40.0);
let b2 = rf(grp.right - 35.0, 12.0, grp.right - 4.0, 40.0);
fill(b1, if dark { rgb(255, 255, 255, 0.14) } else { rgb(0, 0, 0, 0.08) }, 14.0);
icon_sidebar(pt((b1.left + b1.right) / 2.0, (b1.top + b1.bottom) / 2.0), text_c);
unsafe {
let (cx, cy) = ((b2.left + b2.right) / 2.0, (b2.top + b2.bottom) / 2.0);
br().SetColor(&text_c);
let r = app2d::rt();
r.DrawRoundedRectangle(&rr(rf(cx - 8.0, cy - 5.0, cx + 8.0, cy + 6.0), 2.5, 2.5), br(), 1.4, None);
for (p, q) in [((-8.0, 0.5), (-3.0, 0.5)), ((3.0, 0.5), (8.0, 0.5)), ((-3.0, 0.5), (-2.0, 2.5)), ((-2.0, 2.5), (2.0, 2.5)), ((2.0, 2.5), (3.0, 0.5))] {
r.DrawLine(pt(cx + p.0, cy + p.1), pt(cx + q.0, cy + q.1), br(), 1.4, None);
}
}
hit(5, 0, 0, b1);
bar_button(grp);
text(&tr("Calendários"), tf("secg"), rf(20.0, 58.0, side_w - 14.0, 78.0), gray);
let mut y = 84.5;
for (i, c) in a().cals.clone().iter().enumerate() {
let r = rf(10.0, y, side_w - 10.0, y + 31.5);
let box_ = rf(17.5, y + 9.25, 30.5, y + 22.25);
let cc = ev_color(c.color);
if c.on {
fill(box_, cc, 3.0);
check(box_);
} else {
stroke_rect(box_, cc, 3.0, 1.3);
}
text(&c.name, tf("sideg"), rf(41.5, r.top, r.right, r.bottom), text_c);
hit(6, i as i32, 0, r);
y += 31.5;
}
let add = rf(17.5, y + 2.0, side_w - 14.0, y + 30.0);
text(&format!("+ {}", tr("Novo Calendário")), tf("sideg"), add, gray);
hit(7, 0, 0, add);
mini_month(0.0, sz.height, dark, text_c);
}
let (cl, cr) = (x0, sz.width);
let mut plus_l = if a().side { cl + 6.0 } else { 84.0 };
if !a().side {
let sb = rf(84.0, 7.5, 121.0, 44.5);
glass_button(sb, dark, false);
icon_sidebar(pt((sb.left + sb.right) / 2.0, 26.0), text_c);
hit(5, 0, 0, sb);
bar_button(sb);
plus_l = sb.right + 8.0;
}
let plus = rf(plus_l, 7.5, plus_l + 37.0, 44.5);
glass_button(plus, dark, false);
icon_plus(pt((plus.left + plus.right) / 2.0, 26.0), text_c, 7.0);
hit(9, 0, 0, plus);
bar_button(plus);
let names = ["Dia", "Semana", "Mês", "Ano"];
let seg_w = 241.0;
let mid = (cl + cr) / 2.0;
let seg = rf(mid - seg_w / 2.0, 8.0, mid + seg_w / 2.0, 44.0);
glass_button(seg, dark, false);
bar_button(seg);
let iw = (seg_w - 8.0) / 4.0;
for i in 0..4 {
let r = rf(seg.left + 4.0 + i as f32 * iw, 12.0, seg.left + 4.0 + (i + 1) as f32 * iw, 40.0);
if a().view == i {
fill(r, if dark { rgb(255, 255, 255, 0.16) } else { rgb(0, 0, 0, 0.075) }, 14.0);
}
text(&tr(names[i as usize]), tf("seg"), r, text_c);
hit(4, i, 0, r);
}
let srch = rf(cr - 8.0 - 36.0, 8.0, cr - 8.0, 44.0);
glass_button(srch, dark, false);
icon_search(pt((srch.left + srch.right) / 2.0, 26.0), text_c, 1.0);
hit(12, 0, 0, srch);
bar_button(srch);
let an = from_min(a().anchor);
let mon = if a().view == V_YEAR { String::new() } else { mes_cap(an.wMonth as i32) };
let tx = cl + 16.5;
let mw = if mon.is_empty() { 0.0 } else { text_w(tf("titleg"), &mon) + 10.0 };
if !mon.is_empty() {
text(&mon, tf("titleg"), rf(tx, 58.0, tx + mw + 4.0, 100.0), text_c);
}
text(&an.wYear.to_string(), tf("titleyg"), rf(tx + mw, 58.0, tx + mw + 160.0, 100.0), text_c);
let pill = if dark { rgb(255, 255, 255, 0.12) } else { rgb(234, 234, 234, 1.0) };
let nx = rf(cr - 34.5, 69.0, cr - 10.5, 93.0);
let td = rf(nx.left - 71.0, 69.0, nx.left - 6.0, 93.0);
let pv = rf(td.left - 29.5, 69.0, td.left - 5.5, 93.0);
fill(pv, pill, 12.0);
fill(td, pill, 12.0);
fill(nx, pill, 12.0);
text(&tr("Hoje"), tf("seg"), td, text_c);
let cy = 81.0;
let (px, nxx) = ((pv.left + pv.right) / 2.0, (nx.left + nx.right) / 2.0);
line(pt(px + 2.0, cy - 5.0), pt(px - 3.0, cy), text_c, 1.6);
line(pt(px - 3.0, cy), pt(px + 2.0, cy + 5.0), text_c, 1.6);
line(pt(nxx - 2.0, cy - 5.0), pt(nxx + 3.0, cy), text_c, 1.6);
line(pt(nxx + 3.0, cy), pt(nxx - 2.0, cy + 5.0), text_c, 1.6);
hit(3, -1, 0, pv);
hit(3, 0, 0, td);
hit(3, 1, 0, nx);
}
fn paint() {
if a().hol_on {
let an = from_min(a().anchor);
request_holidays(an.wYear as i32);
if an.wMonth == 12 {
request_holidays(an.wYear as i32 + 1);
}
}
if !begin_paint() {
return;
}
a().hits.clear();
if bar() > 0.0 {
paint_golden();
end_paint();
return;
}
let dark = dark();
let sz = size();
let sz2 = (sz.width, sz.height);
let text_c = if dark { rgb(245, 245, 247, 1.0) } else { rgb(29, 29, 31, 1.0) };
let gray = if dark { rgb(152, 152, 157, 1.0) } else { rgb(134, 134, 139, 1.0) };
clear(if dark { rgb(30, 30, 30, 1.0) } else { rgb(255, 255, 255, 1.0) });
let rems = reminders();
let side_w = a().side_w;
let tb = a().tb;
let x0 = if a().side { side_w } else { 0.0 };
paint_content(x0, sz2, dark, text_c, gray, &rems);
if a().side {
fill(rf(0.0, tb, side_w, sz.height), sidebar_color(), 0.0);
fill(rf(side_w - 1.0, tb, side_w, sz.height), if dark { rgb(0, 0, 0, 0.5) } else { rgb(0, 0, 0, 0.08) }, 0.0);
text(&tr("Calendários"), tf("ev"), rf(14.0, tb + 10.0, side_w - 14.0, tb + 30.0), gray);
let mut y = tb + 36.0;
for (i, c) in a().cals.clone().iter().enumerate() {
let r = rf(8.0, y, side_w - 8.0, y + 28.0);
let box_ = rf(r.left + 8.0, y + 7.0, r.left + 22.0, y + 21.0);
let cc = ev_color(c.color);
if c.on {
fill(box_, cc, 3.5);
text("✓", tf("evsmall"), box_, rgb(255, 255, 255, 1.0));
} else {
stroke_rect(box_, cc, 3.5, 1.5);
}
text(&c.name, tf("ev"), rf(r.left + 32.0, r.top, r.right, r.bottom), text_c);
hit(6, i as i32, 0, r);
y += 30.0;
}
let add = rf(14.0, y + 6.0, side_w - 14.0, y + 30.0);
text(&format!("⊕ {}", tr("Novo Calendário")), tf("ev"), add, gray);
hit(7, 0, 0, add);
}
fill(rf(0.0, 0.0, sz.width, tb), if dark { rgb(40, 40, 42, 1.0) } else { rgb(246, 246, 248, 1.0) }, 0.0);
fill(rf(0.0, tb - 1.0, sz.width, tb), if dark { rgb(0, 0, 0, 0.5) } else { rgb(0, 0, 0, 0.1) }, 0.0);
let tog = rf(10.0, 18.0, 40.0, 44.0);
icon_sidebar(pt((tog.left + tog.right) / 2.0, (tog.top + tog.bottom) / 2.0), if a().side { rgb(10, 132, 255, 1.0) } else { text_c });
hit(5, 0, 0, tog);
unsafe {
app2d::rt().SetTransform(&mat::trans(36.0, 0.0));
}
let t0 = a().hits.len();
toolbar((sz.width - 36.0, sz.height), dark, text_c, gray);
unsafe {
app2d::rt().SetTransform(&mat::ident());
}
for h in &mut a().hits[t0..] {
h.r.left += 36.0;
h.r.right += 36.0;
}
end_paint();
}
// ---- ações
fn rename(id: i32) {
let Some(h) = a().hits.iter().find(|h| h.kind == 1 && h.ev == id).copied() else { return };
let titulo = find_ev(id).map(|e| e.title.clone()).unwrap_or_default();
a().editing = id;
field_open(rf(h.r.left + 4.0, h.r.top, h.r.right.max(h.r.left + 160.0), h.r.top + 18.0), &titulo, 12.0, false, move |t, key| {
a().editing = -1;
if find_ev(id).is_none() {
return;
}
if t.is_empty() && key != VK_ESCAPE.0 as i32 {
a().ev.retain(|x| x.id != id);
a().sel_ev = -1;
} else if !t.is_empty() {
find_ev(id).unwrap().title = t;
}
save();
});
invalidate();
}
fn new_event(day: i64, at: i64, all_day: bool) {
let id = a().next_id;
a().next_id += 1;
let mut e = Ev { id, start: if all_day { day } else { at }, end: if all_day { add_days(day, 1) } else { at + 60 }, all_day, title: tr("Novo Evento"), color: 0, notes: String::new(), cal: 1 };
if let Some(c) = a().cals.iter().find(|c| c.on && c.id != HOL_CAL) {
e.cal = c.id;
e.color = c.color;
}
a().ev.push(e);
a().sel_ev = id;
a().sel_day = day;
save();
paint();
rename(id);
}
fn ev_menu(id: i32, scr: POINT) {
let Some(e) = find_ev(id).cloned() else { return };
unsafe {
let (Ok(m), Ok(tm), Ok(dur), Ok(colm), Ok(cal)) = (CreatePopupMenu(), CreatePopupMenu(), CreatePopupMenu(), CreatePopupMenu(), CreatePopupMenu()) else { return };
let ck = |on: bool| if on { MF_CHECKED } else { MF_UNCHECKED };
append_menu(tm, MF_STRING | ck(e.all_day), 100, &tr("Dia Inteiro"));
let _ = AppendMenuW(tm, MF_SEPARATOR, 0, PCWSTR::null());
for h in 7..=21 {
append_menu(tm, MF_STRING, 100 + h, &format!("{h}:00"));
}
let mins = [15, 30, 60, 90, 120, 180];
let dn = ["15 min", "30 min", "1 h", "1 h 30", "2 h", "3 h"];
for i in 0..6 {
append_menu(dur, MF_STRING | if e.all_day { MF_GRAYED } else { MF_ENABLED }, 200 + i, dn[i]);
}
for i in 0..7 {
append_menu(colm, MF_STRING | ck(e.color == i as i32), 300 + i, &tr(COLOR_NAMES[i]));
}
for (i, c) in a().cals.iter().enumerate() {
append_menu(cal, MF_STRING | ck(e.cal == c.id), 500 + i, &c.name);
}
append_menu(m, MF_STRING, 1, &tr("Renomear"));
append_menu(m, MF_POPUP, tm.0 as usize, &tr("Horário"));
append_menu(m, MF_POPUP, dur.0 as usize, &tr("Duração"));
append_menu(m, MF_POPUP, cal.0 as usize, &tr("Calendário"));
append_menu(m, MF_POPUP, colm.0 as usize, &tr("Cor"));
let _ = AppendMenuW(m, MF_SEPARATOR, 0, PCWSTR::null());
append_menu(m, MF_STRING, 2, &tr("Apagar"));
let c = TrackPopupMenu(m, TPM_RETURNCMD | TPM_RIGHTBUTTON, scr.x, scr.y, 0, wnd(), None).0;
let _ = DestroyMenu(m);
let ev = find_ev(id).unwrap();
let day = day_start(ev.start);
match c {
100 => {
ev.all_day = true;
ev.start = day;
ev.end = add_days(day, 1);
}
101..=199 => {
let len = if ev.all_day { 60 } else { (ev.end - ev.start).max(15) };
ev.all_day = false;
ev.start = day + (c - 100) as i64 * 60;
ev.end = ev.start + len;
}
200..=205 => ev.end = ev.start + mins[(c - 200) as usize],
300..=306 => ev.color = c - 300,
2 => {
a().ev.retain(|x| x.id != id);
a().sel_ev = -1;
}
1 => {
paint();
rename(id);
return;
}
x if x >= 500 && ((x - 500) as usize) < a().cals.len() => {
let cc = a().cals[(x - 500) as usize].clone();
ev.cal = cc.id;
ev.color = cc.color;
}
_ => {}
}
if c != 0 {
save();
}
invalidate();
}
}
fn nav(dir: i32) {
let a = a();
if dir == 0 {
a.anchor = day_start(now_min());
a.sel_day = a.anchor;
} else if a.view == V_YEAR {
a.anchor = add_months(a.anchor, dir * 12);
} else if a.view == V_MONTH {
a.anchor = add_months(a.anchor, dir);
} else {
a.anchor = add_days(day_start(a.anchor), dir * if a.view == V_WEEK { 7 } else { 1 });
}
invalidate();
}
fn ics_time(v: &str) -> (i64, bool) {
let all_day = v.len() == 8;
if v.len() < 8 {
return (0, all_day);
}
let n = |a: usize, b: usize| v.get(a..b).and_then(|x| x.parse::<u16>().ok()).unwrap_or(0);
let mut s = SYSTEMTIME { wYear: n(0, 4), wMonth: n(4, 6), wDay: n(6, 8), ..Default::default() };
if v.len() >= 15 {
s.wHour = n(9, 11);
s.wMinute = n(11, 13);
}
if v.ends_with('Z') {
let mut loc = SYSTEMTIME::default();
unsafe {
let _ = SystemTimeToTzSpecificLocalTime(None, &s, &mut loc);
}
s = loc;
}
(to_min(&s), all_day)
}
fn import_ics() {
let Some(path) = pick_file(false, &[("iCalendar (.ics)", "*.ics")], None, None) else { return };
let mut un: Vec<String> = Vec::new();
for l in read_lines(std::path::Path::new(&path)) {
if (l.starts_with(' ') || l.starts_with('\t')) && !un.is_empty() {
un.last_mut().unwrap().push_str(&l[1..]);
} else {
un.push(l);
}
}
let mut n = 0;
let mut cur = Ev::default();
let mut dentro = false;
for l in &un {
let (key, val) = match l.find(':') {
Some(c) => (&l[..c], &l[c + 1..]),
None => (l.as_str(), ""),
};
let k = key.split(';').next().unwrap_or("");
if l == "BEGIN:VEVENT" {
cur = Ev { cal: 1, ..Default::default() };
dentro = true;
} else if l == "END:VEVENT" && dentro {
dentro = false;
if cur.start != 0 {
cur.id = a().next_id;
a().next_id += 1;
if cur.end == 0 || cur.end <= cur.start {
cur.end = if cur.all_day { add_days(cur.start, 1) } else { cur.start + 60 };
}
a().ev.push(cur.clone());
n += 1;
}
} else if dentro && k == "DTSTART" {
let (t, ad) = ics_time(val);
cur.start = t;
cur.all_day = ad || key.contains("VALUE=DATE");
} else if dentro && k == "DTEND" {
cur.end = ics_time(val).0;
} else if dentro && k == "SUMMARY" {
let mut t = String::new();
let mut it = val.chars();
while let Some(c) = it.next() {
if c == '\\' {
if let Some(x) = it.next() {
t.push(if x == 'n' { ' ' } else { x });
continue;
}
}
t.push(c);
}
cur.title = t;
}
}
save();
message_box(&cf!(tr("%d eventos importados."), n), &tr("Calendário"), MB_OK | MB_ICONINFORMATION);
invalidate();
}
fn rename_cal(r: RectF, id: i32, nome: &str) {
field_open(rf(r.left + 32.0, r.top, r.right, r.bottom), nome, 12.0, false, move |t, key| {
if key != VK_ESCAPE.0 as i32 && !t.is_empty() {
if let Some(x) = find_cal(id) {
x.name = t;
save();
}
}
});
}
fn click(h: HWND, m: u32, l: LPARAM) -> LRESULT {
unsafe {
if !app2d::s().field.h.0.is_null() {
field_close();
}
let s_ = sc();
let (x, y) = (get_x_lparam(l) as f32 / s_, get_y_lparam(l) as f32 / s_);
let scr = to_screen(h, get_x_lparam(l), get_y_lparam(l));
let dbl = m == WM_LBUTTONDBLCLK;
let right = m == WM_RBUTTONUP;
let hits = a().hits.clone();
let achar = |k: i32| hits.iter().find(|ht| ht.kind == k && in_rect(&ht.r, x, y)).copied();
if let Some(ht) = achar(4) {
a().view = ht.ev;
invalidate();
return LRESULT(0);
}
if achar(5).is_some() {
a().side = !a().side;
invalidate();
return LRESULT(0);
}
if let Some(ht) = achar(8) {
a().anchor = ht.day;
a().sel_day = ht.day;
a().view = V_DAY;
invalidate();
return LRESULT(0);
}
if achar(7).is_some() {
let c = Cal { id: a().next_id, name: tr("Novo Calendário"), color: (a().cals.len() % 7) as i32, on: true };
a().next_id += 1;
a().cals.push(c.clone());
save();
paint();
let n = a().cals.len() as i32 - 1;
if let Some(k) = a().hits.iter().find(|k| k.kind == 6 && k.ev == n).copied() {
rename_cal(k.r, c.id, &c.name);
}
return LRESULT(0);
}
if let Some(ht) = achar(6) {
let c = a().cals[ht.ev as usize].clone();
if !right {
a().cals[ht.ev as usize].on = !c.on;
save();
if c.id == HOL_CAL && !c.on {
request_holidays(from_min(a().anchor).wYear as i32);
}
invalidate();
return LRESULT(0);
}
if c.id == HOL_CAL {
return LRESULT(0);
}
let (Ok(mm), Ok(colm)) = (CreatePopupMenu(), CreatePopupMenu()) else { return LRESULT(0) };
for i in 0..7 {
append_menu(colm, MF_STRING | if c.color == i as i32 { MF_CHECKED } else { MF_UNCHECKED }, 10 + i, &tr(COLOR_NAMES[i]));
}
append_menu(mm, MF_STRING, 1, &tr("Renomear"));
append_menu(mm, MF_POPUP, colm.0 as usize, &tr("Cor"));
let _ = AppendMenuW(mm, MF_SEPARATOR, 0, PCWSTR::null());
append_menu(mm, MF_STRING | if a().cals.len() > 1 { MF_ENABLED } else { MF_GRAYED }, 2, &tr("Apagar Calendário"));
let k = TrackPopupMenu(mm, TPM_RETURNCMD | TPM_RIGHTBUTTON, scr.x, scr.y, 0, h, None).0;
let _ = DestroyMenu(mm);
let id = c.id;
if k >= 10 {
a().cals[ht.ev as usize].color = k - 10;
for e in a().ev.iter_mut().filter(|e| e.cal == id) {
e.color = k - 10;
}
save();
} else if k == 1 {
rename_cal(ht.r, id, &c.name);
} else if k == 2 {
let n = a().ev.iter().filter(|e| e.cal == id).count();
if message_box(&cf!(tr("Apagar o calendário “%ls” e os %d eventos dele?"), c.name.as_str(), n), &tr("Calendário"), MB_OKCANCEL | MB_ICONWARNING) == IDOK {
a().ev.retain(|e| e.cal != id);
a().cals.remove(ht.ev as usize);
save();
}
}
invalidate();
return LRESULT(0);
}
if let Some(ht) = achar(3) {
nav(ht.ev);
return LRESULT(0);
}
if achar(9).is_some() {
let d = a().sel_day;
new_event(d, d + 9 * 60, a().view == V_MONTH);
return LRESULT(0);
}
if let Some(ht) = achar(10) {
a().sel_day = add_months(month_start(a().sel_day), ht.ev);
invalidate();
return LRESULT(0);
}
if right {
if achar(13).is_some() {
let Ok(mm) = CreatePopupMenu() else { return LRESULT(0) };
let fd = a().first_dow;
append_menu(mm, MF_STRING | if fd == 0 { MF_CHECKED } else { MF_UNCHECKED }, 1, &tr("Começar a Semana no Domingo"));
append_menu(mm, MF_STRING | if fd == 1 { MF_CHECKED } else { MF_UNCHECKED }, 2, &tr("Começar a Semana na Segunda-feira"));
if !a().hol_cc.is_empty() {
let _ = AppendMenuW(mm, MF_SEPARATOR, 0, PCWSTR::null());
append_menu(mm, MF_STRING | if a().hol_on { MF_CHECKED } else { MF_UNCHECKED }, 3, &tr("Mostrar Feriados"));
}
let k = TrackPopupMenu(mm, TPM_RETURNCMD | TPM_RIGHTBUTTON, scr.x, scr.y, 0, h, None).0;
let _ = DestroyMenu(mm);
if k == 1 || k == 2 {
a().first_dow = k - 1;
a().first_dow_set = true;
save();
}
if k == 3 {
if let Some(hc) = find_cal(HOL_CAL) {
hc.on = !hc.on;
let on = hc.on;
save();
if on {
request_holidays(from_min(a().anchor).wYear as i32);
}
}
}
invalidate();
return LRESULT(0);
}
}
if let Some(ht) = achar(11) {
a().anchor = ht.day;
a().sel_day = ht.day;
invalidate();
return LRESULT(0);
}
if let Some(ht) = achar(12) {
let fr = rf(ht.r.left - 200.0, ht.r.top + 8.0, ht.r.left - 8.0, ht.r.bottom - 8.0);
let q0 = a().search.clone();
field_open(fr, &q0, 13.0, false, |q, key| {
a().search = q.clone();
if key != VK_RETURN.0 as i32 || q.is_empty() {
return;
}
let lq = q.to_lowercase();
let sel = a().sel_day;
let mut best: Option<Ev> = None;
for e in &a().ev {
if !e.title.to_lowercase().contains(&lq) {
continue;
}
let melhor = match &best {
None => true,
Some(b) => e.start >= sel && (b.start < sel || e.start < b.start),
};
if melhor {
best = Some(e.clone());
}
}
match best {
Some(b) => {
a().anchor = day_start(b.start);
a().sel_day = a().anchor;
a().sel_ev = b.id;
}
None => {
let _ = MessageBeep(MB_ICONASTERISK);
}
}
invalidate();
});
return LRESULT(0);
}
for ht in hits.iter().rev() {
if ht.kind != 1 || !in_rect(&ht.r, x, y) {
continue;
}
a().sel_ev = ht.ev;
a().sel_day = ht.day;
if ht.ev < 0 {
a().sel_ev = -1;
invalidate();
return LRESULT(0);
}
if right {
invalidate();
ev_menu(ht.ev, scr);
return LRESULT(0);
}
if dbl {
rename(ht.ev);
return LRESULT(0);
}
invalidate();
return LRESULT(0);
}
if let Some(ht) = achar(2) {
a().sel_day = ht.day;
a().sel_ev = -1;
if dbl {
let tb = a().tb;
if a().view == V_MONTH || y < tb + WEEKHEAD + ALLDAY {
new_event(ht.day, 0, true);
} else {
let top = tb + WEEKHEAD + ALLDAY;
let mins = ((y - top + a().scroll_h) / HOUR * 60.0) as i64 / 30 * 30;
new_event(ht.day, ht.day + mins, false);
}
return LRESULT(0);
}
invalidate();
}
LRESULT(0)
}
}
extern "system" fn proc_(h: HWND, m: u32, w_: WPARAM, l: LPARAM) -> LRESULT {
unsafe {
match m {
WM_PAINT => return wm_paint(h, paint),
WM_ERASEBKGND => return LRESULT(1),
WM_SIZE => {
on_size(l);
return LRESULT(0);
}
WM_DPICHANGED => {
on_dpi(w_, l);
return LRESULT(0);
}
WM_SETTINGCHANGE | WM_TIMER => {
invalidate();
return LRESULT(0);
}
WM_CTLCOLOREDIT => return field_colors(HDC(w_.0 as *mut _), if dark() { colorref(30, 30, 30) } else { colorref(255, 255, 255) }),
WM_FIELDLOST => {
let fh = app2d::s().field.h;
if !fh.0.is_null() && GetFocus() != fh {
field_close();
}
return LRESULT(0);
}
WM_HOLIDAYS => {
holidays_arrived(w_.0 as isize as i32, Box::from_raw(l.0 as *mut Vec<HolDay>));
invalidate();
return LRESULT(0);
}
WM_MOUSEWHEEL => {
if !app2d::s().field.h.0.is_null() {
field_close();
}
let d = wheel_delta(w_) / 120.0;
if a().view == V_MONTH {
nav(if d > 0.0 { -1 } else { 1 });
} else {
let mx = (24.0 * HOUR - (size().height - a().tb - WEEKHEAD - ALLDAY)).max(0.0);
a().scroll_h = (a().scroll_h - d * 60.0).clamp(0.0, mx);
invalidate();
}
return LRESULT(0);
}
WM_LBUTTONDOWN | WM_LBUTTONDBLCLK | WM_RBUTTONUP => return click(h, m, l),
WM_KEYDOWN => {
let ctrl = key_down(VK_CONTROL.0);
let k = w_.0 as u32;
if ctrl && ('1' as u32..='4' as u32).contains(&k) {
a().view = (k - '1' as u32) as i32;
invalidate();
} else if ctrl && k == 'I' as u32 {
import_ics();
} else if ctrl && k == 'N' as u32 {
let d = a().sel_day;
new_event(d, d + 9 * 60, a().view == V_MONTH);
} else if ctrl && k == 'W' as u32 {
let _ = DestroyWindow(h);
} else if ctrl && k == 'T' as u32 {
nav(0);
} else if k == VK_LEFT.0 as u32 {
nav(-1);
} else if k == VK_RIGHT.0 as u32 {
nav(1);
} else if (k == VK_DELETE.0 as u32 || k == VK_BACK.0 as u32) && a().sel_ev >= 0 {
let id = a().sel_ev;
a().ev.retain(|x| x.id != id);
a().sel_ev = -1;
save();
invalidate();
} else if k == VK_RETURN.0 as u32 && a().sel_ev >= 0 {
rename(a().sel_ev);
}
return LRESULT(0);
}
WM_DESTROY => {
PostQuitMessage(0);
return LRESULT(0);
}
_ => {}
}
DefWindowProcW(h, m, w_, l)
}
}
fn main() {
if !app2d::init() {
std::process::exit(1);
}
*A.get() = Some(App {
tb: 64.0,
side_w: 200.0,
side: true,
next_id: 1,
view: V_MONTH,
sel_ev: -1,
scroll_h: 8.0 * 48.0,
editing: -1,
hol_on: true,
hol_next_id: -1000,
file: data_path("calendario.txt"),
rem_file: data_path("lembretes.txt"),
..Default::default()
});
if let Some(p) = arg_valor("--dados") {
a().file = p.into();
}
if let Some(v) = arg_valor("--visao") {
a().view = v.parse().unwrap_or(V_MONTH);
}
load();
a().anchor = day_start(now_min());
a().sel_day = a().anchor;
let disp = if has_font("SF Pro Display") { "SF Pro Display".to_string() } else { family() };
let ic = icon_family();
use DWRITE_TEXT_ALIGNMENT_CENTER as C;
use DWRITE_TEXT_ALIGNMENT_LEADING as L;
use DWRITE_TEXT_ALIGNMENT_TRAILING as T;
let f = |s: f32, w_: DWRITE_FONT_WEIGHT, al: DWRITE_TEXT_ALIGNMENT, fam: Option<&str>| fmt_full(s, w_, al, false, fam);
let t = &mut a().tf;
t.insert("month", f(26.0, DWRITE_FONT_WEIGHT_BOLD, L, Some(&disp)));
t.insert("year", f(26.0, DWRITE_FONT_WEIGHT_NORMAL, L, Some(&disp)));
t.insert("btn", f(12.5, DWRITE_FONT_WEIGHT_MEDIUM, C, None));
t.insert("headday", f(12.5, DWRITE_FONT_WEIGHT_MEDIUM, T, None));
t.insert("daynum", f(13.0, DWRITE_FONT_WEIGHT_MEDIUM, T, None));
t.insert("ev", f(11.5, DWRITE_FONT_WEIGHT_NORMAL, L, None));
t.insert("evsmall", f(10.5, DWRITE_FONT_WEIGHT_NORMAL, L, None));
t.insert("yearday", f(10.5, DWRITE_FONT_WEIGHT_NORMAL, C, None));
t.insert("hour", f(10.5, DWRITE_FONT_WEIGHT_NORMAL, T, None));
t.insert("icon", f(14.0, DWRITE_FONT_WEIGHT_NORMAL, C, Some(&ic)));
t.insert("titleg", f(34.0, DWRITE_FONT_WEIGHT_BOLD, L, Some(&disp)));
t.insert("titleyg", f(34.0, DWRITE_FONT_WEIGHT_NORMAL, L, Some(&disp)));
t.insert("seg", f(13.0, DWRITE_FONT_WEIGHT_NORMAL, C, None));
t.insert("sideg", f(13.0, DWRITE_FONT_WEIGHT_NORMAL, L, None));
t.insert("secg", f(11.0, DWRITE_FONT_WEIGHT_BOLD, L, None));
t.insert("minit", f(13.0, DWRITE_FONT_WEIGHT_SEMI_BOLD, C, None));
t.insert("minid", f(12.5, DWRITE_FONT_WEIGHT_MEDIUM, C, None));
t.insert("miniw", f(11.5, DWRITE_FONT_WEIGHT_SEMI_BOLD, C, None));
set_bar_wanted(52.0);
let aq = semaforo::aqua();
if aq {
a().tb = 112.0;
a().side_w = 218.5;
}
let Some(h) = create_main("CalendarioJanela", &tr("Calendário"), proc_, if aq { 1137 } else { 1100 }, 760) else { std::process::exit(1) };
unsafe {
SetTimer(h, 1, 30000, None);
}
load_holiday_cache();
show(h, SW_SHOWNORMAL);
std::process::exit(run());
}