% \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{beamerouterthememoloch}[2025-12-18 v2.1.0 Moloch outer theme]
%</package>
% \fi
% \CheckSum{0}
% \StopEventually{}
% \iffalse
%<*package>
% ------------------------------------------------------------------------- \fi
%
% \subsection{Moloch Outer Theme}
%
% A \verb|beamer| outer theme dictates the style of the frame elements traditionally
% set outside the body of each slide: the head, footline, and frame title.
%
%
%
% \subsection{Package Dependencies}
%
%    \begin{macrocode}
\RequirePackage{calc}
\RequirePackage{pgfopts}
%    \end{macrocode}
%
%
% \subsection{Memoization and Tikz Externalization}
%
% To avoid generating externalized figures of the progressbar we have to disable
% them with ``tikzexternalenable'' and ``tikzexternaldisable''. However, if the
% ``external'' library is not loaded we would get undefined control sequence
% problems, hence we define them as no-ops if they are not defined yet.
% We do the same for the ``mmzUnmemoizable'' command from the memoize package, in
% order to avoid memoization of the progress bars.
%
%    \begin{macrocode}
\providecommand{\tikzexternalenable}{}
\providecommand{\tikzexternaldisable}{}
\providecommand{\mmzUnmemoizable}{}
%    \end{macrocode}
%
% \subsection{Options}
%
% \begin{macro}{progressbar}
%    Adds a progress bar to the top, bottom, or frametitle of each slide.
%    \begin{macrocode}
\pgfkeys{
  /moloch/outer/progressbar/.cd,
  .is choice,
  none/.code={%
      \setbeamertemplate{headline}[plain]
      \setbeamertemplate{frametitle}[plain]
      \setbeamertemplate{footline}[plain]
    },
  head/.code={\pgfkeys{/moloch/outer/progressbar=none}
      \addtobeamertemplate{headline}{}{%
        \usebeamertemplate*{progress bar in head/foot}
      }
    },
  frametitle/.code={\pgfkeys{/moloch/outer/progressbar=none}
      \addtobeamertemplate{frametitle}{}{%
        \usebeamertemplate*{progress bar in head/foot}
      }
    },
  foot/.code={\pgfkeys{/moloch/outer/progressbar=none}
      \addtobeamertemplate{footline}{}{%
        \usebeamertemplate*{progress bar in head/foot}%
      }
    },
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{progressbar linewidth}
%    Sets the linewidth of the progress bar for sectionpages and frames.
%    \begin{macrocode}
\newlength{\moloch@progressonsectionpage}
\newlength{\moloch@progressonsectionpage@linewidth}
\newlength{\moloch@progressinheadfoot}
\newlength{\moloch@progressinheadfoot@linewidth}
\pgfkeys{
  /moloch/outer/.cd,
  progressbarlinewidth/.code={
      \setlength{\moloch@progressonsectionpage@linewidth}{#1}
      \setlength{\moloch@progressinheadfoot@linewidth}{#1}
    },
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{progressbar aliases}
%    Allows \verb|progressbar linewidth| to be used in \verb|\molochset|.
%    \begin{macrocode}
\pgfkeys{
  /moloch/outer/.cd,
  progressbar linewidth/.code=\pgfkeysalso{progressbarlinewidth=#1},
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{frametitle margin}
%    Sets the margins of the frame title.
%    \begin{macrocode}
\pgfkeys{
  /moloch/outer/.cd,
  frametitlemarginleft/.code=\renewcommand{\moloch@frametitle@margin@left}{#1},
  frametitlemarginright/.code=\renewcommand{\moloch@frametitle@margin@right}{#1},
  frametitlemargintop/.code=\renewcommand{\moloch@frametitle@margin@top}{#1},
  frametitlemarginbottom/.code=\renewcommand{\moloch@frametitle@margin@bottom}{#1},
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{frametitle margin aliases}
%   Allows \verb|frametitle margin x| to be used in \verb|\molochset|.
%   \begin{macrocode}
\pgfkeys{
  /moloch/outer/.cd,
  frametitle margin left/.code=\pgfkeysalso{frametitlemarginleft=#1},
  frametitle margin right/.code=\pgfkeysalso{frametitlemarginright=#1},
  frametitle margin top/.code=\pgfkeysalso{frametitlemargintop=#1},
  frametitle margin bottom/.code=\pgfkeysalso{frametitlemarginbottom=#1},
}
%   \end{macrocode}
%
% \begin{macro}{\moloch@outer@setdefaults}
%    Sets default values for outer theme options.
%    \begin{macrocode}
\newcommand{\moloch@outer@setdefaults}{
  \pgfkeys{/moloch/outer/.cd,
    progressbar=none,
    progressbar linewidth=1pt,
  }
}
%    \end{macrocode}%
% \end{macro}
%
% \subsection{Deprecated Options}
%
% These options are deprecated and will be removed in a future version.
%
% \begin{macro}{numbering}
%    Adds slide numbers to the bottom right of each slide.
%    \begin{macrocode}
\pgfkeys{
  /moloch/outer/numbering/.cd,
  .is choice,
  none/.code={%
      \PackageWarning{moloch}{The ``numbering'' option is deprecated.
        Use beamer's ``page number in head/foot'' template instead}%
      \setbeamertemplate{page number in head/foot}[default]
    },
  counter/.code={%
      \PackageWarning{moloch}{The ``numbering'' option is deprecated.
        Use beamer's ``page number in head/foot'' template instead}%
      \setbeamertemplate{page number in head/foot}[framenumber]
    },
  fraction/.code={%
      \PackageWarning{moloch}{The ``numbering'' option is deprecated.
        Use beamer's ``page number in head/foot'' template instead}%
      \setbeamertemplate{page number in head/foot}[totalframenumber]
    },
}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Slide Numbering}
%
% Moloch defaults to numbering frames. To modify this, simply copy this line to your
% preamble and replace \verb|framenumber|.
%
%    \begin{macrocode}
\setbeamertemplate{page number in head/foot}[framenumber]
%    \end{macrocode}
%
% \subsection{Head and Footline}
%
% All good \verb|beamer| presentations should already remove the navigation symbols,
% but Moloch removes them automatically (just in case).
%
%    \begin{macrocode}
\setbeamertemplate{navigation symbols}{}
%    \end{macrocode}
%
%
% \begin{macro}{headline}
%
%   Template for the headline at the top of each frame, which is empty by default.
%
%    \begin{macrocode}
\defbeamertemplate{headline}{plain}{}
%    \end{macrocode}
%
% \end{macro}
%
% \begin{macro}{footline}
%
%    Template for the footline at the bottom of each frame, which includes the
%    slide number on the right.
%
%    \begin{macrocode}
\defbeamertemplate{footline}{plain}{%
  \begin{beamercolorbox}[
      leftskip=4pt,%
      rightskip=5pt,%
      wd=\textwidth,%
    ]{footline}%
    \usebeamercolor[fg]{page number in head/foot}%
    \usebeamerfont{page number in head/foot}%
    \usebeamertemplate*{frame footer}%
    \hfill%
    \usebeamertemplate*{page number in head/foot}\vskip4pt%
  \end{beamercolorbox}%
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \subsection{Frametitle}
%
% \begin{macro}{frametitle}
%    Templates for the frame title, which is optionally underlined with a
%    progress bar.
%    \begin{macrocode}
\newcommand{\moloch@frametitlestrut@start}{%
  \rule{0pt}{\moloch@frametitle@margin@top + \ht\strutbox}%
}%

\newcommand{\moloch@frametitlestrut@end}{%
  \rule[-\moloch@frametitle@margin@bottom]{0pt}{\moloch@frametitle@margin@bottom}%
}

\newcommand{\moloch@frametitle@margin@left}{1.6ex}
\newcommand{\moloch@frametitle@margin@right}{1.6ex}
\newcommand{\moloch@frametitle@margin@top}{1.4ex}
\newcommand{\moloch@frametitle@margin@bottom}{1.4ex}

\defbeamertemplate{frametitle}{plain}{%
  \nointerlineskip%
  \begin{beamercolorbox}[%
      wd=\paperwidth,%
      leftskip=\moloch@frametitle@margin@left,%
      rightskip=\the\glueexpr \moloch@frametitle@margin@right plus 1fil\relax,%
    ]{frametitle}%
    \usebeamerfont{frametitle}%
    \moloch@frametitlestrut@start%
    \moloch@frametitleformat{\insertframetitle}%
    {%
      \ifx\insertframesubtitle\@empty%
        \else%
        {%
          \par%
          \usebeamerfont{framesubtitle}%
          \vspace{-0.8ex}%
          \usebeamercolor[fg]{framesubtitle}%
          \insertframesubtitle%
        }%
      \fi
    }%
    \moloch@frametitlestrut@end%
  \end{beamercolorbox}%
}
\setbeamertemplate{frametitle continuation}{%
  \romannumeral\insertcontinuationcount}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{progress bar in head/foot}
%    Template for the progress bar optionally displayed below the frame title
%    on each page. Much of this code is duplicated in the inner theme's
%    template \verb|progress bar in section page|.
%    \begin{macrocode}
\setbeamertemplate{progress bar in head/foot}{
  \nointerlineskip%
  \pgfmathsetlength{\moloch@progressinheadfoot}{%
    \paperwidth * min(1,\insertframenumber/\inserttotalframenumber)%
  }%
  \begin{beamercolorbox}[wd=\paperwidth]{progress bar in head/foot}
    \tikzexternaldisable%
    \begin{tikzpicture}
      \mmzUnmemoizable%
      \fill[bg]
      (0,0)
      rectangle
      (\paperwidth, \moloch@progressinheadfoot@linewidth);
      \fill[fg]
      (0,0)
      rectangle
      (\moloch@progressinheadfoot, \moloch@progressinheadfoot@linewidth);
    \end{tikzpicture}
    \tikzexternalenable%
  \end{beamercolorbox}
}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Process Package Options}
%
%    \begin{macrocode}
\moloch@outer@setdefaults
\ProcessPgfPackageOptions{/moloch/outer}
%    \end{macrocode}
%
% \iffalse
%</package>
% \fi
% \Finale
\endinput
