%% psipresentation.cls
\ProvidesClass{psipresentation}[2024/05/28 Unofficial PSI Presentation Class by Sven Augustin]
\NeedsTeXFormat{LaTeX2e}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{beamer}}
\ProcessOptions\relax

\LoadClass[final, 10pt, aspectratio=169, xcolor=table]{beamer} % t: align columns to top
\beamertemplatenavigationsymbolsempty



\institute{Paul Scherrer Institut PSI}

\newcommand\centerfor[1]{\def\insertcenterfor{#1}}
\let\insertcenterfor\empty

\newcommand\location[1]{\def\insertlocation{#1}}
\location{}

\newcommand\titleimage[1]{\def\inserttitleimage{#1}}
\let\inserttitleimage\empty

\newcommand\titlecolor[1]{\def\inserttitlecolor{#1}}
\titlecolor{blue}



\RequirePackage{polyglossia}
\RequirePackage{microtype}
\RequirePackage{fontspec}
\RequirePackage{csquotes}
\RequirePackage{tikz}



\setmainlanguage{english}

\setbeamersize{
  text margin left=0.055\paperwidth,
  text margin right=0.055\paperwidth
}

\setmainfont{Aptos}[
Path = psipresentation/fonts/Aptos/,
Extension = .ttf,
UprightFont = *-Regular,
BoldFont = *-Bold,
ItalicFont = *-Italic,
BoldItalicFont = *-Bold-Italic
]

\setsansfont{Aptos}[
Path = psipresentation/fonts/Aptos/,
Extension = .ttf,
UprightFont = *-Regular,
BoldFont = *-Bold,
ItalicFont = *-Italic,
BoldItalicFont = *-Bold-Italic
]

\setmonofont{AptosMono}[
Path = psipresentation/fonts/AptosMono/,
Extension = .ttf,
UprightFont = *-Regular,
BoldFont = *-Bold,
ItalicFont = *-Italic,
BoldItalicFont = *-Bold-Italic
]



\input{psipresentation/psicolors}



% boxes

\setbeamerfont{block title}{series=\bfseries, size=\normalsize}
\setbeamercolor{block title}{fg=black, bg=white}



% lists

\setlength{\leftmargini}{10pt}
\setlength{\leftmarginii}{10pt}
\setlength{\leftmarginiii}{10pt}

\newcommand\ibullet{\raisebox{-0.2em}{\scalebox{2.2}{$\cdot$}}}

\setbeamertemplate{itemize item}{\ibullet}
\setbeamertemplate{itemize subitem}{\ibullet}
\setbeamertemplate{itemize subsubitem}{\ibullet}

\setbeamercolor{itemize item}{fg=black}
\setbeamercolor{itemize subitem}{fg=black}
\setbeamercolor{itemize subsubitem}{fg=black}

\setbeamercolor{enumerate item}{fg=black}
\setbeamercolor{enumerate subitem}{fg=black}
\setbeamercolor{enumerate subsubitem}{fg=black}



% title page

\newcommand{\TPtextleftedge}{0.056}

\newcommand{\TPlogowidth}{0.14}
\newcommand{\TPlogoupperedge}{0.84}

\newcommand{\TPbottomboxheight}{0.27}

\newcommand{\TPimageheight}{0.78}

\newcommand{\TPtitleypos}{4.65}
\newcommand{\TPsubtitleypos}{3.8}
\newcommand{\TPauthorypos}{1.1}
\newcommand{\TPlocationdateypos}{0.75}

\newcommand{\TPcenterforwidth}{0.195}
\newcommand{\TPcenterforleftedge}{0.208}
\newcommand{\TPcenterforupperedge}{0.8615}

\colorlet{TPauthorfont}{white}
\colorlet{TPdatefont}{white}


\defbeamertemplate*{title page}{PSI}[1][]
{
  \begin{tikzpicture}[overlay, remember picture, shift={(current page.south west)}] % use the whole page

    \ifx \inserttitleimage \empty

      \colorlet{TPtitlefont}{white}
      \colorlet{TPsubtitlefont}{white}
      \newcommand{\TPlogocolor}{white}
      \newcommand{\TPtextwidth}{0.66}

      \node[inner sep=0pt, anchor=south west] (bkg) at (-0.01, -0.01) {
        \includegraphics[height=\paperheight]{psipresentation/imgs/bkgs/\inserttitlecolor}
      };

    \else

      \colorlet{TPtitlefont}{black}
      \colorlet{TPsubtitlefont}{black}
      \newcommand{\TPlogocolor}{black}
      \newcommand{\TPtextwidth}{0.43}

      \fill[\inserttitlecolor] (0, \TPbottomboxheight\paperheight) rectangle ++(\paperwidth, -\paperheight); % bottom color box

      \node[inner sep=0pt, anchor=west] (image) at (\paperwidth/2 + 5, \paperheight/2 + 5) {
        \includegraphics[height=\TPimageheight\paperheight]{\inserttitleimage}
      };

    \fi

    \node[inner sep=0pt, anchor=south west] (psi-logo) at (\TPtextleftedge\paperwidth, \TPlogoupperedge\paperheight) {
      \includegraphics[width=\TPlogowidth\paperwidth]{psipresentation/imgs/logos/psi-logo-\TPlogocolor}
    };

    \ifx \insertcenterfor \empty \else
      \node[inner sep=0pt, anchor=south west] (centerfortext) at (\TPcenterforleftedge\paperwidth, \TPcenterforupperedge\paperheight) {
        \rlap{\fontsize{6}{6}\selectfont\textbf{\textcolor{TPtitlefont}{%
          \parbox[t]{\TPcenterforwidth\paperwidth}{%
            Center for \insertcenterfor%
          }
        }}}};
      \fi

    %TODO: make the following a command?

    \node[inner sep=0pt, anchor=south] (titletext) at (\TPtextleftedge\paperwidth, \TPtitleypos) {
      \rlap{\huge\textbf{\textcolor{TPtitlefont}{%
        \parbox[t]{\TPtextwidth\paperwidth}{%
          \inserttitle%
        }
      }}}};

    \node[inner sep=0pt] (subtitletext) at (\TPtextleftedge\paperwidth, \TPsubtitleypos) {
      \rlap{\large\textbf{\textcolor{TPsubtitlefont}{%
        \parbox[t]{\TPtextwidth\paperwidth}{%
          \insertsubtitle{}%
        }
      }}}};

    \node[inner sep=0pt] (authortext) at (\TPtextleftedge\paperwidth, \TPauthorypos) {
      \rlap{\footnotesize{\textcolor{TPauthorfont}{%
        \parbox[t]{\TPtextwidth\paperwidth}{%
          \insertauthor{}%
        }
      }}}};

    \node[inner sep=0pt] (locationdatetext) at (\TPtextleftedge\paperwidth, \TPlocationdateypos) {
       \rlap{\footnotesize{\textcolor{TPdatefont}{
        \parbox[t]{\TPtextwidth\paperwidth}{%
          \insertlocation, \insertdate%
        }
      }}}};

  \end{tikzpicture}
}



% frame

\newcommand{\FRheaderheight}{0.09}

\newcommand{\FRlogowidth}{0.075}
\newcommand{\FRlogoupperedge}{0.96}
\newcommand{\FRlogorightedge}{0.94}
\newcommand{\FRlogocolor}{black}

\newcommand{\FRtitleupperedge}{0.95}
\newcommand{\FRtitleleftedge}{0.055}
\newcommand{\FRtitlewidth}{0.75}

\newcommand{\FRfooterupperedge}{0.06}
\newcommand{\FRfooterauthorleftedge}{\FRtitleleftedge}
\newcommand{\FRfooterinstituteleftedge}{0.12}
\newcommand{\FRfooterdaterightedge}{0.94} % 1 - 0.06

\colorlet{FRtitlefont}{black}
\colorlet{FRfooterfont}{black}


\setbeamertemplate{frametitle}
{
  \begin{tikzpicture}[overlay, remember picture, shift={(current page.south west)}] % use the whole page

    \node[inner sep=0pt, anchor=north east] (psi-logo) at (\FRlogorightedge\paperwidth, \FRlogoupperedge\paperheight) {
      \includegraphics[width=\FRlogowidth\paperwidth]{psipresentation/imgs/logos/psi-logo-\FRlogocolor}
    };

    \node[inner sep=0pt, anchor=north west] (frame-title) at (\FRtitleleftedge\paperwidth, \FRtitleupperedge\paperheight) {
      \parbox[t]{\FRtitlewidth\paperwidth}{%
        \textcolor{FRtitlefont}{\large\textbf{\insertframetitle}}%
      }
    };

    \node[inner sep=0pt, anchor=west] (page-num) at (\FRfooterauthorleftedge\paperwidth, \FRfooterupperedge\paperheight) {
      \textcolor{FRfooterfont}{\tiny\insertframenumber}
    };

    \node[inner sep=0pt, anchor=west] (institute) at (\FRfooterinstituteleftedge\paperwidth, \FRfooterupperedge\paperheight) {
      \textcolor{FRfooterfont}{\tiny \ifx \insertcenterfor \empty \insertinstitute \else PSI \insertcenterfor \fi}
    };

    \node[inner sep=0pt, anchor=east] (author) at (\FRfooterdaterightedge\paperwidth, \FRfooterupperedge\paperheight) {
      \textcolor{FRfooterfont}{\tiny\insertdate}
    };

  \end{tikzpicture}

  % since the above is all overlay, simply add enough space to cover the head
  \vskip\FRheaderheight\paperheight
}



% section divider

\newenvironment{divider}[2]{
  % #1 background color
  % #2 font and logo color (white or black)

  \setbeamercolor{background canvas}{bg=#1}

  \colorlet{FRtitlefont}{#2}
  \colorlet{FRfooterfont}{#2}

  \setbeamercolor{normal text}{fg=#2}
  \usebeamercolor[fg]{normal text}

  \renewcommand{\FRlogocolor}{#2} % can only be black or white

  \begin{frame}[environment=divider,t]{\empty}
  \huge
}{
  \end{frame}
}



% colored tables

\newenvironment{ctabular}[3]{
  % #1 tabular options
  % #2 row background color
  % #3 header font color

  \newcolumntype{L}{>{\ifnumequal{\rownum}{1}{\bfseries\color{#3}}}l}
  \newcolumntype{C}{>{\ifnumequal{\rownum}{1}{\bfseries\color{#3}}}c}
  \newcolumntype{R}{>{\ifnumequal{\rownum}{1}{\bfseries\color{#3}}}r}

  \rowcolors{2}{#2!20}{#2!10}
  \begin{tabular}{#1}
    \rowcolor{#2}
}{
  \end{tabular}
}



