From a3532d6a73a79fc143316bbe6e5e291d8c611e95 Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Sat, 31 Oct 2026 22:01:11 +0300 Subject: [PATCH] Open a downloaded game on Lichess or Chess.com MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The game screen has a globe after the result and an item in ☰ when the game's Link or Site is a web address: a Lichess game opens at the move shown, from the side at the bottom of the board; a Chess.com game (its Link tag) at its page. The game list's context menu opens it too. Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/game_link.rs | 123 ++++++++++++++++++++++++++++++++++ apps/desktop/src/game_view.rs | 50 ++++++++++++-- apps/desktop/src/i18n.rs | 55 +++++++++++++++ apps/desktop/src/large_db.rs | 11 +++ apps/desktop/src/lib.rs | 1 + 5 files changed, 236 insertions(+), 4 deletions(-) create mode 100644 apps/desktop/src/game_link.rs diff --git a/apps/desktop/src/game_link.rs b/apps/desktop/src/game_link.rs new file mode 100644 index 0000000..fa56f44 --- /dev/null +++ b/apps/desktop/src/game_link.rs @@ -0,0 +1,123 @@ +//! The game's page on the site it was played on: Lichess writes it in the Site tag +//! («https://lichess.org/AbCdEfGh»), Chess.com in the Link tag (its Site is +//! «Chess.com»). A Lichess game opens at the move shown and from the side at the +//! bottom of the board; Chess.com's page has no such address, it opens at the start. + +use crate::i18n::tr; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Site { + Lichess, + ChessCom, + /// Another web address. + Other, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct GameLink { + pub url: String, + pub site: Site, + /// The Lichess game id (8 characters), when the address is a game's. + lichess_id: Option, +} + +/// The game's web address from its tags (Link first, then Site), if it has one. +pub fn of(tag: impl Fn(&str) -> Option) -> Option { + let url = ["Link", "Site"] + .into_iter() + .filter_map(&tag) + .map(|v| v.trim().to_owned()) + .find(|v| v.starts_with("https://") || v.starts_with("http://"))?; + let host_and_path = url.split_once("://").map_or("", |(_, rest)| rest); + let (host, path) = host_and_path.split_once('/').unwrap_or((host_and_path, "")); + let host = host.to_ascii_lowercase(); + let host = host.strip_prefix("www.").unwrap_or(&host); + let site = match host { + "lichess.org" => Site::Lichess, + "chess.com" => Site::ChessCom, + _ => Site::Other, + }; + // «AbCdEfGh», or the 12-character id of one player's view of it. + let first = path.split(['/', '#', '?']).next().unwrap_or(""); + let lichess_id = (site == Site::Lichess + && (first.len() == 8 || first.len() == 12) + && first.chars().all(|c| c.is_ascii_alphanumeric())) + .then(|| first[..8].to_owned()); + Some(GameLink { + url, + site, + lichess_id, + }) +} + +impl GameLink { + /// The address to open: a Lichess game at half-move `ply` (0: the start) from + /// Black's side when `black`; any other as written. + pub fn at(&self, ply: u32, black: bool) -> String { + match &self.lichess_id { + Some(id) => { + let mut url = format!("https://lichess.org/{id}"); + if black { + url.push_str("/black"); + } + if ply > 0 { + url.push_str(&format!("#{ply}")); + } + url + } + None => self.url.clone(), + } + } + + /// «🌐 Open on Lichess». + pub fn label(&self) -> &'static str { + match self.site { + Site::Lichess => tr("🌐 Open on Lichess"), + Site::ChessCom => tr("🌐 Open on Chess.com"), + Site::Other => tr("🌐 Open the game's link"), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn link(tags: &[(&str, &str)]) -> Option { + of(|name| { + tags.iter() + .find(|(n, _)| *n == name) + .map(|(_, v)| (*v).to_owned()) + }) + } + + #[test] + fn lichess_chess_com_and_others() { + let l = link(&[("Site", "https://lichess.org/AbCdEfGh")]).expect("lichess"); + assert_eq!(l.site, Site::Lichess); + assert_eq!(l.at(0, false), "https://lichess.org/AbCdEfGh"); + assert_eq!(l.at(23, true), "https://lichess.org/AbCdEfGh/black#23"); + // One player's view (12 characters): the game. + let l = link(&[("Site", "https://lichess.org/AbCdEfGh1234")]).unwrap(); + assert_eq!(l.at(4, false), "https://lichess.org/AbCdEfGh#4"); + // A study is opened as it is. + let s = link(&[("Site", "https://lichess.org/study/abcdefgh/ijklmnop")]).unwrap(); + assert_eq!( + s.at(10, true), + "https://lichess.org/study/abcdefgh/ijklmnop" + ); + let c = link(&[ + ("Site", "Chess.com"), + ("Link", "https://www.chess.com/game/live/123456789"), + ]) + .expect("chess.com"); + assert_eq!(c.site, Site::ChessCom); + assert_eq!(c.at(12, true), "https://www.chess.com/game/live/123456789"); + assert_eq!( + link(&[("Site", "http://example.org/g/1")]).unwrap().site, + Site::Other + ); + assert!(link(&[("Site", "Moscow RUS")]).is_none()); + assert!(link(&[("Site", "Chess.com")]).is_none()); + } +} diff --git a/apps/desktop/src/game_view.rs b/apps/desktop/src/game_view.rs index 4e54fa6..08a129c 100644 --- a/apps/desktop/src/game_view.rs +++ b/apps/desktop/src/game_view.rs @@ -328,6 +328,17 @@ impl GameView { } } + /// The game's web page, if its tags have one. + fn link(&self) -> Option { + crate::game_link::of(|t| self.game.tag(t).map(str::to_owned)) + } + + /// Opens the game's page at the move shown, from the side at the bottom. + fn open_link(&self, ctx: &egui::Context, link: &crate::game_link::GameLink) { + let ply = self.game.node(self.cursor).map_or(0, |n| n.ply()); + ctx.open_url(egui::OpenUrl::new_tab(link.at(ply, self.flipped))); + } + fn subtitle(&self) -> String { if let Some(t) = &self.train { let up_to = trf( @@ -1020,17 +1031,34 @@ impl GameView { None => (None, ""), }, }; - let result_w = result.as_ref().map_or(0.0, |r| { + // The game's page on Lichess or Chess.com: a globe after the result. + let link = self.link(); + let link_w = if link.is_some() { ui.painter() .layout_no_wrap( - format!(" {r} "), - egui::FontId::proportional(15.0), + "🌐".to_owned(), + egui::TextStyle::Button.resolve(ui.style()), Color32::WHITE, ) .size() .x + + 2.0 * ui.spacing().button_padding.x + gap - }); + } else { + 0.0 + }; + let result_w = link_w + + result.as_ref().map_or(0.0, |r| { + ui.painter() + .layout_no_wrap( + format!(" {r} "), + egui::FontId::proportional(15.0), + Color32::WHITE, + ) + .size() + .x + + gap + }); let row = ui.available_width(); let compact = row - left(false) - right(false) - result_w < 160.0; let title_room = (row - left(compact) - right(compact)).max(0.0); @@ -1105,6 +1133,14 @@ impl GameView { ) .on_hover_text(result_hint); } + if let Some(l) = &link + && ui + .small_button("🌐") + .on_hover_text(format!("{} — {}", l.label(), tr("in the browser"))) + .clicked() + { + self.open_link(ui.ctx(), l); + } }); let sub: Vec = self .source_line() @@ -1988,6 +2024,12 @@ impl GameView { actions.push(GameAction::AddToDatabase); ui.close(); } + if let Some(l) = self.link() + && ui.button(l.label()).clicked() + { + self.open_link(ui.ctx(), &l); + ui.close(); + } ui.separator(); if ui.button(tr("Copy PGN")).clicked() { actions.push(self.copy_pgn()); diff --git a/apps/desktop/src/i18n.rs b/apps/desktop/src/i18n.rs index 96c6398..6adefc2 100644 --- a/apps/desktop/src/i18n.rs +++ b/apps/desktop/src/i18n.rs @@ -16089,6 +16089,61 @@ const ROWS: &[[&str; 9]] = &[ "oxirgi {n}", "соңғы {n}", ], + [ + "🌐 Open on Lichess", + "🌐 Открыть на Lichess", + "🌐 Ouvrir sur Lichess", + "🌐 Auf Lichess öffnen", + "🌐 Abrir en Lichess", + "🌐 Apri su Lichess", + "🌐 Abrir no Lichess", + "🌐 Lichess’da ochish", + "🌐 Lichess-те ашу", + ], + [ + "🌐 Open on Chess.com", + "🌐 Открыть на Chess.com", + "🌐 Ouvrir sur Chess.com", + "🌐 Auf Chess.com öffnen", + "🌐 Abrir en Chess.com", + "🌐 Apri su Chess.com", + "🌐 Abrir no Chess.com", + "🌐 Chess.com’da ochish", + "🌐 Chess.com-да ашу", + ], + [ + "🌐 Open the game's link", + "🌐 Открыть ссылку партии", + "🌐 Ouvrir le lien de la partie", + "🌐 Link der Partie öffnen", + "🌐 Abrir el enlace de la partida", + "🌐 Apri il link della partita", + "🌐 Abrir o link da partida", + "🌐 Partiya havolasini ochish", + "🌐 Партия сілтемесін ашу", + ], + [ + "🌐 Open in the browser", + "🌐 Открыть в браузере", + "🌐 Ouvrir dans le navigateur", + "🌐 Im Browser öffnen", + "🌐 Abrir en el navegador", + "🌐 Apri nel browser", + "🌐 Abrir no navegador", + "🌐 Brauzerda ochish", + "🌐 Браузерде ашу", + ], + [ + "in the browser", + "в браузере", + "dans le navigateur", + "im Browser", + "en el navegador", + "nel browser", + "no navegador", + "brauzerda", + "браузерде", + ], ]; #[cfg(test)] diff --git a/apps/desktop/src/large_db.rs b/apps/desktop/src/large_db.rs index 0be27f7..d93d60d 100644 --- a/apps/desktop/src/large_db.rs +++ b/apps/desktop/src/large_db.rs @@ -2260,6 +2260,17 @@ impl LargeView { opened = Some(index); ui.close(); } + // A game downloaded from Lichess or Chess.com: its page. + let online = r.site.to_ascii_lowercase().contains("lichess.org") + || r.site.trim().eq_ignore_ascii_case("chess.com"); + if online && ui.button(tr("🌐 Open in the browser")).clicked() { + if let Some(link) = db.load_game(index).and_then(|p| { + crate::game_link::of(|t| p.game.tag(t).map(str::to_owned)) + }) { + ui.ctx().open_url(egui::OpenUrl::new_tab(link.at(0, false))); + } + ui.close(); + } // One item per player of the game (the same name once). let mut players: Vec<&str> = vec![r.white.as_str(), r.black.as_str()]; players.dedup(); diff --git a/apps/desktop/src/lib.rs b/apps/desktop/src/lib.rs index ed0ecbf..af68f60 100644 --- a/apps/desktop/src/lib.rs +++ b/apps/desktop/src/lib.rs @@ -24,6 +24,7 @@ mod engine_offer; mod engines; mod examples; mod file_state; +mod game_link; mod game_view; #[cfg(not(web))] mod graphics;