% !TeX root = test.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 http://www.latex-project.org/lppl.txt
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{beamerinnerthemetcolorbox}[2022/09/06 version v0.4 Beamer blocks with tcolorboxes]

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

\tcbset{
  beamer@tcb@iftitle/.code n args={1}{
    \ifbeamer@tcb@title
      \pgfkeysalso{#1}
    \fi
  }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Theme Options
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifbeamer@tcb@rounded
\newif\ifbeamer@tcb@shadow
\newif\ifbeamer@tcb@shaded
\newif\ifbeamer@tcb@title
\newif\ifbeamer@tcb@inmargin
\def\beamer@tcb@true{true}

\DeclareOptionBeamer{shadow}[true]{\csname beamer@tcb@shadow#1\endcsname}
\DeclareOptionBeamer{rounded}[true]{\csname beamer@tcb@rounded#1\endcsname\csname beamer@tcb@shaded#1\endcsname}
\DeclareOptionBeamer{shaded}[true]{\csname beamer@tcb@shaded#1\endcsname}
\DeclareOptionBeamer{inmargin}[true]{\csname beamer@tcb@inmargin#1\endcsname}

\ExecuteOptionsBeamer{shadow=false}
\ExecuteOptionsBeamer{rounded=false}
\ExecuteOptionsBeamer{shaded=false}
\ExecuteOptionsBeamer{inmargin=false}

% check options from other themes
\ifdefined\beamer@themerounded@shadow
  \beamer@tcb@roundedtrue
  \beamer@tcb@shadedtrue
  \ifx\beamer@themerounded@shadow\beamer@tcb@true
    \beamer@tcb@shadowtrue
  \fi
\fi

\ifdefined\beamer@putleft
  \beamer@tcb@inmargintrue
\fi

\ProcessOptionsBeamer

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Colours
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\beamer@tcb@colini}[1][]{
  \usebeamercolor{block body#1} 
  \colorlet{beamer@tcb@bodybg}{block body#1.bg}
  \colorlet{beamer@tcb@bodyfg}{block body#1.fg}
  \usebeamercolor{block title#1} 
  \colorlet{beamer@tcb@titlebg}{block title#1.bg}
  \colorlet{beamer@tcb@titlefg}{block title#1.fg}  
  \usebeamercolor{normal text}
}
\beamer@tcb@colini

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% General Settings
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tcbset{
  oversize=0mm
}

\tcbsetforeverylayer{
  left=0.05cm,
  right=0.05cm,
  enhanced,
  boxrule=0cm,
  frame hidden,
  top=1.5mm,
  bottom=0.5mm,
  colback=beamer@tcb@bodybg,
  colbacktitle=beamer@tcb@titlebg,
  coltext=beamer@tcb@bodyfg,
  coltitle=beamer@tcb@titlefg,
}

% optional settings
\ifbeamer@tcb@rounded
  \tcbsetforeverylayer{
    arc=1.3mm,
    top=0.8mm,
    toptitle=0.2mm,
    bottom=0mm,
  }
\else
  \tcbsetforeverylayer{sharpish corners}
  \beamer@tcb@titletrue
\fi

\ifbeamer@tcb@shaded
  \tcbsetforeverylayer{
    title code app={
      \shade[top color=beamer@tcb@titlebg,bottom color=beamer@tcb@bodybg] (title.south west) rectangle ([yshift=-0.5mm]title.south east); 
    }    
  }
\fi

\ifbeamer@tcb@shadow
  \tcbsetforeverylayer{drop fuzzy shadow=black!90}
\fi

\ifbeamer@tcb@inmargin
  \tcbsetforeverylayer{
    detach title, 
    overlay={
      \node[anchor=north east,align=right,text width=\dimexpr\beamer@leftsidebar-\beamer@leftmargin-\beamer@rightmargin\relax] at ([xshift=-\beamer@leftmargin]frame.north west) {\tcbtitle};
    }
  }
\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Defining blocks
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>

% normal block
\setbeamertemplate{block begin}{%
  \beamer@tcb@colini
  \ifx\insertblocktitle\@empty\else
    \beamer@tcb@titletrue
  \fi
  \begin{tcolorbox}[
    colback=beamer@tcb@bodybg,
    colbacktitle=beamer@tcb@titlebg,
    coltext=beamer@tcb@bodyfg,
    coltitle=beamer@tcb@titlefg,
    before title={\usebeamerfont{block title}},
    before upper={\usebeamercolor{block body}\usebeamerfont{block body}},
    beamer@tcb@iftitle={title={\insertblocktitle}}
  ]
} 
\setbeamertemplate{block end}{\end{tcolorbox}}

% alert block
\setbeamertemplate{block alerted begin}{%
  \beamer@tcb@colini[ alerted]
  \ifx\insertblocktitle\@empty\else
    \beamer@tcb@titletrue
  \fi
  \begin{tcolorbox}[
    colback=beamer@tcb@bodybg,
    colbacktitle=beamer@tcb@titlebg,
    coltext=beamer@tcb@bodyfg,
    coltitle=beamer@tcb@titlefg,
    before title={\usebeamerfont{block title alerted}},
    before upper={\usebeamercolor{block body alerted}\usebeamerfont{block body alerted}},    
    beamer@tcb@iftitle={title={\insertblocktitle}}
  ]
} 
\setbeamertemplate{block alerted end}{\end{tcolorbox}}

% example block
\setbeamertemplate{block example begin}{%
  \beamer@tcb@colini[ example]
  \ifx\insertblocktitle\@empty\else
    \beamer@tcb@titletrue
  \fi
  \begin{tcolorbox}[
    colback=beamer@tcb@bodybg,
    colbacktitle=beamer@tcb@titlebg,
    coltext=beamer@tcb@bodyfg,
    coltitle=beamer@tcb@titlefg,
    before title={\usebeamerfont{block title example}},
    before upper={\usebeamercolor{block body example}\usebeamerfont{block body example}},      
    beamer@tcb@iftitle={title={\insertblocktitle}}
  ]
} 
\setbeamertemplate{block example end}{\end{tcolorbox}}

\mode<all>
