All files / src/lib/components PomodoroTimer.svelte

100% Statements 82/82
70.83% Branches 17/24
100% Functions 16/16
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223              10x 10x 10x 10x 10x   10x           10x     11x 10x   33x     1x   33x         11x 10x 10x               11x                           10x 11x 10x 2x   8x         10x 13x 2x   11x 2x 1x             2x 11x   10x     10x 11x                                                                                                                                                                                                                                                                          
<script lang="ts">
  import { pomodoro } from '../stores/pomodoro.svelte'
  import { todos } from '../stores/todos.svelte'
  import { history } from '../stores/history.svelte'
  import { settings } from '../stores/settings.svelte'
  import type { TimerMode } from '../types'
 
  const SIZE = 240
  const STROKE = 12
  const RADIUS = (SIZE - STROKE) / 2
  const CIRCUMFERENCE = 2 * Math.PI * RADIUS
  const t = settings.t
 
  const modes = $derived([
    { key: 'work' as TimerMode, label: t('work') },
    { key: 'short-break' as TimerMode, label: t('shortBreak') },
    { key: 'long-break' as TimerMode, label: t('longBreak') },
  ])
 
  const dashoffset = $derived(CIRCUMFERENCE * (1 - pomodoro.progress))
</script>
 
<div class="timer-panel" style="--mode-color: var(--accent-{pomodoro.mode === 'work' ? 'work' : pomodoro.mode === 'short-break' ? 'short' : 'long'})">
  <div class="mode-tabs">
    {#each modes as m}
      <button
        class="mode-tab"
        class:active={pomodoro.mode === m.key}
        onclick={() => pomodoro.setMode(m.key)}
      >
        {m.label}
      </button>
    {/each}
  </div>
 
  <div class="clock-wrap" role="timer" aria-live="off" aria-label="{pomodoro.label} {t('min')} - {t(pomodoro.mode === 'work' ? 'work' : pomodoro.mode === 'short-break' ? 'shortBreak' : 'longBreak')}">
    <svg class="clock-svg" viewBox="0 0 {SIZE} {SIZE}">
      <circle
        cx={SIZE / 2}
        cy={SIZE / 2}
        r={RADIUS}
        fill="none"
        stroke="var(--track)"
        stroke-width={STROKE}
      />
      <circle
        cx={SIZE / 2}
        cy={SIZE / 2}
        r={RADIUS}
        fill="none"
        stroke="var(--mode-color)"
        stroke-width={STROKE}
        stroke-linecap="round"
        stroke-dasharray={CIRCUMFERENCE}
        stroke-dashoffset={dashoffset}
        transform="rotate(-90 {SIZE / 2} {SIZE / 2})"
        style="transition: stroke-dashoffset 0.5s linear"
      />
    </svg>
    <div class="clock-label">
      <span class="time">{pomodoro.label}</span>
      {#if todos.activeTask}
        <span class="active-task" title={todos.activeTask.title}>{todos.activeTask.title}</span>
      {:else}
        <span class="active-task muted">{t('semTarefa')}</span>
      {/if}
    </div>
  </div>
 
  <div class="controls">
    {#if pomodoro.running}
      <button class="btn btn-secondary" onclick={() => pomodoro.pause()}>{t('pause')}</button>
    {:else}
      <button class="btn btn-primary" onclick={() => {
        if (todos.activeTask) {
          history.addEntry({
            taskId: todos.activeTask.id,
            taskTitle: todos.activeTask.title,
            mode: pomodoro.mode,
            type: 'play'
          })
        }
        pomodoro.start()
      }}>{t('start')}</button>
    {/if}
    <button class="btn btn-ghost" onclick={() => pomodoro.reset()}>{t('resetTimer')}</button>
  </div>
 
  <p class="pomodoro-count">
    🍅 {pomodoro.workCount} {pomodoro.workCount === 1 ? t('pomodoroToday') : t('pomodorosToday')}
  </p>
</div>
 
<style>
  .timer-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
    padding: 2rem;
  }
 
  .mode-tabs {
    display: flex;
    gap: 0.5rem;
    background: var(--surface);
    border-radius: 8px;
    padding: 4px;
  }
 
  .mode-tab {
    padding: 0.4rem 1rem;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.875rem;
    color: var(--text-muted);
    transition: all 0.2s;
  }
 
  .mode-tab.active {
    background: var(--mode-color);
    color: #fff;
    font-weight: 600;
  }
 
  .clock-wrap {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
  }
 
  .clock-svg {
    width: 240px;
    height: 240px;
    max-width: 100%;
  }
 
  .clock-label {
    position: absolute;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
  }
 
  .time {
    font-size: 3rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--text);
  }
 
  .active-task {
    font-size: 0.75rem;
    color: var(--text-muted);
    max-width: 150px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
 
  .controls {
    display: flex;
    gap: 0.75rem;
  }
 
  .btn {
    padding: 0.6rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s, transform 0.1s;
  }
 
  .btn:active {
    transform: scale(0.97);
  }
 
  .btn-primary {
    background: var(--mode-color);
    color: #fff;
  }
 
  .btn-secondary {
    background: var(--surface);
    color: var(--text);
  }
 
  .btn-ghost {
    background: transparent;
    color: var(--text-muted);
  }
 
  .btn:hover {
    opacity: 0.85;
  }
 
  .pomodoro-count {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin: 0;
  }
 
  @media (max-width: 640px) {
    .timer-panel {
      padding: 1rem;
      gap: 1rem;
    }
    .clock-svg {
      width: 200px;
      height: 200px;
    }
    .time {
      font-size: 2.5rem;
    }
  }
</style>