% !TeX root = ./testing/talktest.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% The tcolorbox inner theme
% A beamer inner theme which reproduces standard beamer blocks using tcolorboxes
% Maintained by samcarter
%
% Project repository and bug tracker:
% https://github.com/samcarter/beamertheme-tcolorbox
%
% Released under the LaTeX Project Public License v1.3c or later
% See https://www.latex-project.org/lppl.txt
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{talkthemetcolorbox}[2025/10/08 version v1.2 Beamer blocks with tcolorboxes]

\RequirePackage{tcolorbox}
\tcbuselibrary{skins,hooks}

\providecommand\@secondofthree[3]{#2}

\tcbset{
  talk@tcb@if/.code n args = {3}{%
    \csname #1\endcsname\expandafter\@secondofthree\fi
    \@secondoftwo{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}%
  },
  talk@tcb@iffirstlayer/.code 2 args = {%
    \ifnum\c@tcblayer<2
      \expandafter\@secondoftwo
    \fi
    \@secondoftwo{\pgfkeysalso{#1}}{\pgfkeysalso{#2}}%
  }
}

\newif\iftalk@tcb@rounded
\newif\iftalk@tcb@shadow
\newif\iftalk@tcb@shaded
\newif\iftalk@tcb@showtitle
\newif\iftalk@tcb@titlepage
\newif\iftalk@tcb@blocks

\newcommand*{\tcbthemeoptions}{block}
\newcommand{\tcbthemeset}[1]{%
  \tcbset{block/.append style={#1}}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Theme Options
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{xkeyval}
\DeclareOptionX{shadow}[true]{
  \csname talk@tcb@shadow#1\endcsname
}
\DeclareOptionX{rounded}[true]{
  \csname talk@tcb@rounded#1\endcsname
  \csname talk@tcb@shaded#1\endcsname
  \csname talk@tcb@showtitlefalse\endcsname
}
\DeclareOptionX{shaded}[true]{
  \csname talk@tcb@shaded#1\endcsname
}
\DeclareOptionX{showtitle}[true]{
  \csname talk@tcb@showtitle#1\endcsname
}
\DeclareOptionX{titlepage}[true]{
  \csname talk@tcb@titlepage#1\endcsname
}
\DeclareOptionX{blocks}[true]{
  \csname talk@tcb@blocks#1\endcsname
}
\ExecuteOptionsX{
  shadow=false,
  rounded=false,
  shaded=false,
  showtitle=false,
  titlepage=true,
  blocks=true
}
\ProcessOptionsX

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% General Settings
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tcbset{
  block/.style={
    enhanced,
    before title={\strut},
    before upper={\strut},
    left=0.05cm,
    right=0.05cm,
    boxrule=0cm,
    frame hidden,
    top=1.5mm,
    bottom=0.5mm,
    talk@tcb@iffirstlayer={oversize=0mm}{},
    colbacktitle=structure!75!black,
    coltitle=white,
    colback=tcbcolbacktitle!10,
    talk@tcb@if={iftalk@tcb@rounded}{
      arc=1.3mm,
      top=0.8mm,
      toptitle=0.2mm,
      bottom=0mm
    }{
      sharpish corners
    },
    talk@tcb@if={iftalk@tcb@shaded}{
      title code app={
          \shade[top color=tcbcolbacktitle,bottom color=tcbcolback,opacity=\kvtcb@opacitybacktitle] (title.south west) rectangle ([yshift=-0.5mm]title.south east);
        }
    }{},
    talk@tcb@if={iftalk@tcb@shadow}{
      drop fuzzy shadow=black!90
    }{}
  },
  alertblock/.style={
    block,
    colbacktitle=alert!75!black,
    colback=tcbcolbacktitle!10,
  },
  exampleblock/.style={
    block,
    colbacktitle=example!75!black,
    colback=tcbcolbacktitle!10,
  },
  titlepage/.style={
    halign=center,
    enhanced,
    frame hidden,
    colback=white,
    talk@tcb@if={iftalk@tcb@rounded}{
      arc=1.3mm,
    }{
      sharpish corners
    }
  },
  titlepage-title/.style={
    titlepage,
    fontupper=\Large,
    colback=structure,
    colupper=white,
    boxsep=4mm,
    talk@tcb@if={iftalk@tcb@shadow}{
      drop fuzzy shadow=black!90
    }{}
  },
  titlepage-author/.style={
    titlepage
  },
  titlepage-institute/.style={
    titlepage,
    fontupper=\scriptsize
  },
  titlepage-date/.style={
    titlepage
  },
  beamer hidden/.style={
    opacityfill=0,
    invisible,
    no shadow
  }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Defining blocks
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\iftalk@tcb@blocks
  \RenewTColorBox{block}{ d<> O{} m }{
    block,
    talk@tcb@if={iftalk@tcb@showtitle}{title={\mbox{}#3}}{title={#3}},
    #2,
    alt=<#1>{}{beamer hidden}
  }
  \NewTColorBox{alertblock}{ d<> O{} m }{
    alertblock,
    talk@tcb@if={iftalk@tcb@showtitle}{title={\mbox{}#3}}{title={#3}},
    #2,
    alt=<#1>{}{beamer hidden}
  }
  \NewTColorBox{exampleblock}{ d<> O{} m }{
    exampleblock,
    talk@tcb@if={iftalk@tcb@showtitle}{title={\mbox{}#3}}{title={#3}},
    #2,
    alt=<#1>{}{beamer hidden}
  }
\fi % if blocks

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% patch title page etc.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ExplSyntaxOn
\iftalk@tcb@titlepage

  \DeclareTemplateInterface{titlepage-element}{talk}{1}{
    after-skip : length = 0em ,
    before-skip : length = 0em ,
    tag-begin : tokenlist = ,
    tag-end : tokenlist = ,
    instance : tokenlist =
  }

  \DeclareTemplateCode{titlepage-element}{talk}{1}{
    after-skip  = \l__talk_titlelem_after_skip ,
    before-skip = \l__talk_titlelem_before_skip ,
    tag-begin   = \l__talk_titlelem_tag_begin_tl ,
    tag-end     = \l__talk_titlelem_tag_end_tl ,
    instance    = \l__talk_titlelem_instance_tl ,
  }{
    \tl_if_empty:nF {#1}{
      \vspace{\l__talk_titlelem_before_skip}
      \group_begin:
        \l__talk_titlelem_tag_begin_tl
        \begin{tcolorbox}[titlepage-\l__talk_titlelem_instance_tl]
          #1
          \str_if_eq:VnT \l__talk_titlelem_instance_tl {title}{
            \tl_if_empty:NF \@subtitle {
              \par\medskip\large\@subtitle\par
            }
          }
        \end{tcolorbox}
        \l__talk_titlelem_tag_end_tl
      \group_end:
      \vspace{\l__talk_titlelem_after_skip}
    }
  }

  \DeclareInstance{titlepage-element}{author}{talk}{
    instance = author
  }
  \DeclareInstance{titlepage-element}{date}{talk}{
    instance = date
  }
  \DeclareInstance{titlepage-element}{institute}{talk}{
    instance = institute
  }
  \DeclareInstance{titlepage-element}{title}{talk}{
    instance = title
  }

  \EditTemplateDefaults{titlepage}{talk}{
    element-order = {title, author, institute, date}
  }

\fi % if titlepage
\ExplSyntaxOff

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Avoid tagging warning for tcolorboxes
% copied from u-fischer https://github.com/latex3/tagging-project/issues/73#issuecomment-2089027746
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\AddToHook{env/tcolorbox/before}{\par\tagstructbegin{tag=Div}}
\AddToHook{env/tcolorbox/begin}{\tagpdfparaOff\tcbset{before upper=\tagpdfparaOn}}
\AddToHook{env/tcolorbox/after}{\par\tagstructend}
\AddToHook{env/block/before}{\par\tagstructbegin{tag=Div}}
\AddToHook{env/block/begin}{\tagpdfparaOff\tcbset{before upper=\tagpdfparaOn}}
\AddToHook{env/block/after}{\par\tagstructend}
\AddToHook{env/alertblock/before}{\par\tagstructbegin{tag=Div}}
\AddToHook{env/alertblock/begin}{\tagpdfparaOff\tcbset{before upper=\tagpdfparaOn}}
\AddToHook{env/alertblock/after}{\par\tagstructend}
\AddToHook{env/exampleblock/before}{\par\tagstructbegin{tag=Div}}
\AddToHook{env/exampleblock/begin}{\tagpdfparaOff\tcbset{before upper=\tagpdfparaOn}}
\AddToHook{env/exampleblock/after}{\par\tagstructend}
