% \iffalse meta-comment -------------------------------------------------------
% Copyright 2015 Matthias Vogelgesang and the LaTeX community. A full list of
% contributors can be found at
%
%     https://github.com/matze/mtheme/graphs/contributors
%
% and the original template was based on the HSRM theme by Benjamin Weiss.
%
% This work is licensed under a Creative Commons Attribution-ShareAlike 4.0
% International License (https://creativecommons.org/licenses/by-sa/4.0/).
%% --------------------------------------------------------------------------- 
%% Copyright 2024 Johan Larsson and contributors
% ------------------------------------------------------------------------- \fi
% \iffalse
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerinnerthememoloch}[2024-07-09 v0.4.0 Moloch inner theme] % x-release-please-version
%</package>
% \fi
% \CheckSum{0}
% \StopEventually{}
% \iffalse
%<*package>
% ------------------------------------------------------------------------- \fi
%
% \subsection{\themename inner theme}
%
% A |beamer| inner theme dictates the style of the frame elements traditionally
% set in the ``body'' of each slide. These include:
%
% \begin{itemize}
%   \item title, part, and section pages;
%   \item itemize, enumerate, and description environments;
%   \item block environments including theorems and proofs;
%   \item figures and tables; and
%   \item footnotes and plain text.
% \end{itemize}
%
%
%
% \subsubsection{Package dependencies}
%
%    \begin{macrocode}
\RequirePackage{keyval}
\RequirePackage{calc}
\RequirePackage{pgfopts}
\RequirePackage{tikz}
%    \end{macrocode}
%
%
%
% \subsubsection{Options}
%
% \begin{macro}{sectionpage}
%    Optionally add a slide marking the beginning of each section.
%    \begin{macrocode}
\pgfkeys{
  /moloch/inner/sectionpage/.cd,
  .is choice,
  none/.code=\moloch@disablesectionpage,
  simple/.code={%
      \moloch@enablesectionpage%
      \setbeamertemplate{section page}[simple]%
    },
  progressbar/.code={%
      \moloch@enablesectionpage%
      \setbeamertemplate{section page}[progressbar]%
    },
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{subsectionpage}
%    Optionally add a slide marking the beginning of each subsection.
%    \begin{macrocode}
\pgfkeys{
  /moloch/inner/subsectionpage/.cd,
  .is choice,
  none/.code=\moloch@disablesubsectionpage,
  simple/.code={%
      \moloch@enablesubsectionpage%
      \setbeamertemplate{section page}[simple]%
    },
  progressbar/.code={%
      \moloch@enablesubsectionpage%
      \setbeamertemplate{section page}[progressbar]%
    },
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\moloch@inner@setdefaults}
% Set default values for inner theme options.
%    \begin{macrocode}
\newcommand{\moloch@inner@setdefaults}{
  \pgfkeys{/moloch/inner/.cd,
    sectionpage=progressbar,
    subsectionpage=none
  }
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsubsection{Title page}
%
% \begin{macro}{title page}
% Template for the title page. Each element is only typset if it is defined
% by the user. If |\subtitle| is empty, for example, it won't leave a blank
% space on the title slide.
%    \begin{macrocode}
\setbeamertemplate{title page}{
  \begin{minipage}[b][\paperheight]{\textwidth}
    \null%
    \vfill%
    \ifx\inserttitlegraphic\@empty\else\usebeamertemplate*{title graphic}\fi
    \ifx\inserttitle\@empty\else\usebeamertemplate*{title}\fi
    \ifx\insertsubtitle\@empty\else\usebeamertemplate*{subtitle}\fi
    \usebeamertemplate*{title separator}
    \expandafter\ifblank\expandafter{\beamer@andstripped}{}{%
      \usebeamertemplate*{author}%
    }
    \ifx\insertinstitute\@empty\else\usebeamertemplate*{institute}\fi
    \ifx\insertdate\@empty\else\usebeamertemplate*{date}\fi
    \vfill
    \null
  \end{minipage}%
}
%    \end{macrocode}
% \end{macro}%
%
% Normal people should use |\maketitle| or |\titlepage| instead of using the
% |title page| beamer template directly. Beamer already defines these macros,
% but we patch them here to make the title page |[plain]| by default, remove
% |\@thanks|, and ensure the title frame number doesn't count.
%
% \begin{macro}{\maketitle}
% \begin{macro}{\titlepage}
%
%   Inserts the title frame, or causes the current frame to use the
%   |title page| template.
%
%    \begin{macrocode}
\def\maketitle{%
  \ifbeamer@inframe
    \titlepage
  \else
    \frame[plain,noframenumbering]{\titlepage}
  \fi
}
\def\titlepage{%
  \usebeamertemplate{title page}
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{title graphic}
%   Set the title graphic in a zero-height box, so it doesn't change the
%   position of other elements.
%    \begin{macrocode}
\setbeamertemplate{title graphic}{
  \inserttitlegraphic%
  \par%
  \vspace*{1em}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{title}
%   Set the title on the title page.
%    \begin{macrocode}
\setbeamertemplate{title}{
  \raggedright%
  \moloch@titleformat{\inserttitle}%
  \par%
  \vspace*{0.2em}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{subtitle}
%   Set the subtitle on the title page.
%    \begin{macrocode}
\setbeamertemplate{subtitle}{
  \vspace*{0.1em}
  \raggedright%
  \moloch@subtitleformat{\insertsubtitle}%
  \par%
  \vspace*{0.2em}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{title separator}
%   Template to set the title graphic in a zero-height box. (It won't
%   change the position of other elements.)
%    \begin{macrocode}
\newlength{\moloch@titleseparator@linewidth}
\setlength{\moloch@titleseparator@linewidth}{0.4pt}
\setbeamertemplate{title separator}{
  \tikzexternaldisable%
  \begin{tikzpicture}
    \mmzUnmemoizable%
    \fill[fg] (0,0) rectangle (\textwidth, \moloch@titleseparator@linewidth);
  \end{tikzpicture}%
  \tikzexternalenable%
  \par%
  \vspace*{0.8em}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{author}
%   Set the author on the title page.
%    \begin{macrocode}
\setbeamertemplate{author}{
  \raggedright%
  \insertauthor%
  \par%
  \vspace*{0.5em}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{institute}
%   Set the institute on the title page.
%    \begin{macrocode}
\setbeamertemplate{institute}{
  \insertinstitute%
  \par%
  \vspace*{1em}
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{date}
%   Set the date on the title page.
%    \begin{macrocode}
\setbeamertemplate{date}{
  \insertdate%
  \par%
}
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Section page}
%
% \begin{macro}{section page}
%
%   Template for the section title slide at the beginning of each section.
%
%    \begin{macrocode}
\defbeamertemplate{section page}{simple}{
  \begin{center}
    \usebeamercolor[fg]{section title}
    \usebeamerfont{section title}
    \moloch@sectiontitleformat{\insertsectionhead}\par
    \ifx\insertsubsectionhead\@empty\else
      \usebeamercolor[fg]{subsection title}
      \usebeamerfont{subsection title}
      \insertsubsectionhead
    \fi
  \end{center}
}
\defbeamertemplate{section page}{progressbar}{
  \centering
  \begin{minipage}{0.7875\linewidth}
    \raggedright
    \usebeamercolor[fg]{section title}
    \usebeamerfont{section title}
    \moloch@sectiontitleformat{\insertsectionhead}\\[-1ex]
    \usebeamertemplate*{progress bar in section page}
    \par
    \ifx\insertsubsectionhead\@empty\else%
      \usebeamercolor[fg]{subsection title}%
      \usebeamerfont{subsection title}%
      \insertsubsectionhead
    \fi
  \end{minipage}
  \par
  \vspace{\baselineskip}
}
\newcommand{\moloch@disablesectionpage}{
  \AtBeginSection{
    % intentionally empty
  }
}
\newcommand{\moloch@enablesectionpage}{
  \AtBeginSection{
    \ifbeamer@inframe
      \sectionpage
    \else
      \frame[plain,c,noframenumbering]{\sectionpage}
    \fi
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{subsection page}
%
%   Template for the subsection title slide that can optionally be added to
%   at the beginning of each subsection.
%
%    \begin{macrocode}
\setbeamertemplate{subsection page}{%
  \usebeamertemplate*{section page}
}
\newcommand{\moloch@disablesubsectionpage}{
  \AtBeginSubsection{
    % intentionally empty
  }
}
\newcommand{\moloch@enablesubsectionpage}{
  \AtBeginSubsection{
    \ifbeamer@inframe
      \subsectionpage
    \else
      \frame[plain,c,noframenumbering]{\subsectionpage}
    \fi
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{progress bar in section page}
%
%   Template for the progress bar displayed by default on the section page.
%   This code is duplicated in large part in the outer theme's template
%   |progress bar in head/foot|.
%
%    \begin{macrocode}
\newlength{\moloch@progressonsectionpage}
\newlength{\moloch@progressonsectionpage@linewidth}
\setlength{\moloch@progressonsectionpage@linewidth}{0.4pt}
\setbeamertemplate{progress bar in section page}{
  \pgfmathsetlength{\moloch@progressonsectionpage}{
    \textwidth * min(1,\insertframenumber/\inserttotalframenumber)
  }%
  \tikzexternaldisable%
  \begin{tikzpicture}
    \mmzUnmemoizable%
    \fill[bg]
    (0,0)
    rectangle
    (\textwidth, \moloch@progressonsectionpage@linewidth);
    \fill[fg]
    (0,0)
    rectangle
    (\moloch@progressonsectionpage, \moloch@progressonsectionpage@linewidth);
  \end{tikzpicture}
  \tikzexternalenable%
}
%    \end{macrocode}
%
%   The above code assumes that |\insertframenumber| is less than or equal to
%   |\inserttotalframenumber|. However, this is not true on the first compile;
%   in the absence of an |.aux| file, |\inserttotalframenumber| defaults to 1.
%   This behaviour could cause fatal errors for long presentations, as
%   |\moloch@progressonsectionpage| would exceed \TeX's maximum length
%   (16383.99999pt, roughly 5.75 metres or 18.9 feet).
%   To avoid this, we increase the default value for |\inserttotalframenumber|;
%   presentations with over 4000 slides will still break on first compile, but
%   users in that situation likely have deeper problems to solve.
%
%    \begin{macrocode}
\def\inserttotalframenumber{100}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Lists and floats}
%
%    \begin{macrocode}
\setbeamertemplate{itemize item}{\(\bullet\)}
\setbeamertemplate{itemize subitem}{\(\circ\)}
\setbeamertemplate{itemize subsubitem}{\textbullet}
\setbeamertemplate{caption label separator}{: }
\setbeamertemplate{caption}[numbered]
%    \end{macrocode}
%
%
%
% \subsubsection{Footnotes}
%    \begin{macrocode}
\setbeamertemplate{footnote}{%
  \parindent 0em\noindent%
  \raggedright
  \usebeamercolor{footnote}%
  \hbox to 0.8em{\hfil\insertfootnotemark}%
  \insertfootnotetext\par%
}
%    \end{macrocode}
%
%
%
% \subsubsection{Text and spacing settings}
%
%
% By default, Beamer frames offer the |c| option to \textit{almost} vertically
% center the text, but the placement is a little too high. To fix this, we
% redefine the |c| option to equalize |\beamer@frametopskip| and
% |\beamer@framebottomskip|. This solution was suggested by Enrico Gregorio in
% an answer to \href{http://tex.stackexchange.com/questions/247826/}{this
% Stack Exchange question}.
%
%    \begin{macrocode}
\define@key{beamerframe}{c}[true]{% centered
  \beamer@frametopskip=0pt plus 1fill\relax%
  \beamer@framebottomskip=0pt plus 1fill\relax%
  \beamer@frametopskipautobreak=0pt plus .4\paperheight\relax%
  \beamer@framebottomskipautobreak=0pt plus .6\paperheight\relax%
  \def\beamer@initfirstlineunskip{}%
}
%    \end{macrocode}
%
%
%
% \subsubsection{Standout frames}
%
% \themename offers a custom frame format with large, centered text and an
% inverted background. To use it, add the key |standout| to the frame:
%
% |\begin{frame}[standout] ... \end{frame}|.
%
% \begin{macro}{standout}
%
%     Optional arguments to Beamer's frames are implemented using
%     |\define@key| from the |keyval| package, which will execute code when the
%     defined option is called. For the |standout| option, we begin a group,
%     change the colors and set frame options.
%
%    \begin{macrocode}
\providebool{moloch@standout}
\define@key{beamerframe}{standout}[true]{%
  \booltrue{moloch@standout}
  \begingroup
  \setkeys{beamerframe}{c}
  \setkeys{beamerframe}{noframenumbering}
  \ifbeamercolorempty[bg]{palette primary}{
    \setbeamercolor{background canvas}{
      use=palette primary,
      bg=-palette primary.fg
    }
  }{
    \setbeamercolor{background canvas}{
      use=palette primary,
      bg=palette primary.bg
    }
  }
  \setbeamercolor{local structure}{
    fg=palette primary.fg
  }
  \usebeamercolor[fg]{palette primary}
}
%    \end{macrocode}
%
%    Then we just have to close the group after the standout slide is finished
%    in order to restore the colours and fonts for the rest of the
%    presentation. Unfortunately, we cannot use \AfterEndEnvironment{frame} for
%    this (see \url{http://tex.stackexchange.com/questions/226319/}).
%    Instead, we prepend the |\endgroup| to |\beamer@reseteecodes|, which is run
%    exactly once at the end of each slide.
%
%    \begin{macrocode}
\pretocmd{\beamer@reseteecodes}{%
  \ifbool{moloch@standout}{
    \endgroup
    \boolfalse{moloch@standout}
  }{}
}{}{}
%    \end{macrocode}
%
%    We set the fonts and the \centering alignment on the inner content,
%    in such a way that the speaker's note layout isn't affected by the custom
%    formatting.
%
%    \begin{macrocode}
\AtBeginEnvironment{beamer@frameslide}{
  \ifbool{moloch@standout}{
    \centering
    \usebeamerfont{standout}
  }{}
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Process package options}
%
%    \begin{macrocode}
\moloch@inner@setdefaults
\ProcessPgfPackageOptions{/moloch/inner}
%    \end{macrocode}
%
% \iffalse
%</package>
% \fi
% \Finale
\endinput
